php
  • html
  • function
  • 2011-08-20 80 views 0 likes 
    0

    这是我在PHP第一功能下面是代码帮助将HTML功能

    <html><head> 
    <style type="text/css"> 
    .tright 
    { 
    float:right; 
    margin:5px; 
    } 
    .tleft 
    { 
    float:left; 
    margin:5px; 
    } 
    </style> 
    </head><body> 
    <?php 
    $fullstring = '[p1]{{15em}}((tleft)) 
    \\Biography// 
    [[Name==My Name]] 
    [[Fathers Name==My Father Name]] 
    [[Mothers Name==My Mother Name]] 
    [[Date Of Birth==My Date Of Birth]] 
    
    [/p1][p1]{{15em}}((tright)) 
    \\Biography// 
    [[Name==My Name]] 
    [[Fathers Name==My Father Name]] 
    [[Mothers Name==My Mother Name]] 
    [[Date Of Birth==My Date Of Birth]] 
    
    [/p1]'; 
    
    
    function get_string_between($string, $start, $end){ 
        $string = " ".$string; 
        $ini = strpos($string,$start); 
        if ($ini == 0) return ""; 
        $ini += strlen($start); 
        $len = strpos($string,$end,$ini) - $ini; 
        return substr($string,$ini,$len); 
    } 
    function Make_html($texttoprase){ 
    //$parsed = get_string_between($texttoprase, "[p1]", "[/p1]"); 
    $mywidth = get_string_between($texttoprase, "{{", "}}"); 
    //replacing width variables 
    $texttoprase = str_replace("{{","",$texttoprase); 
    $texttoprase = str_replace("}}","",$texttoprase); 
    $texttoprase = str_replace($mywidth,"",$texttoprase); 
    //getiing class for table 
    $myclass = get_string_between($texttoprase, "((", "))"); 
    //replacing class variables 
    $texttoprase = str_replace("((","",$texttoprase); 
    $texttoprase = str_replace("))","",$texttoprase); 
    $texttoprase = str_replace($myclass,"",$texttoprase); 
    $texttoprase = str_replace("[[","<tr><th scope=\"row\" style=\"text-align:left;\">",$texttoprase); 
    $texttoprase = str_replace("==","</th><td>",$texttoprase); 
    $texttoprase = str_replace("]]","</td></tr>",$texttoprase); 
    $texttoprase = str_replace("\\","<tr><td colspan=\"2\" style=\"text-align:center;\">",$texttoprase); 
    $texttoprase = str_replace("//","</td></tr>",$texttoprase); 
    return "<div class=\"" . $myclass . "\"><table style=\"width: " . $mywidth . ";\" cellspacing=\"5\"><tbody>" . $texttoprase . "</tbody></table></div>"; 
    } 
    $texttofind = get_string_between($fullstring, "[p1]", "[/p1]"); 
    $textToReplace = Make_html($texttofind) ; 
    $fullstring = str_replace("[p1]" . $texttofind . "[/p1]", $textToReplace ,$fullstring); 
    echo $fullstring; 
    ?></body></html> 
    

    它,在[P1]/[P1],但如何第一次出现的HTML,使第二任何帮助或想法?

    +1

    如果您使用了HTML解析器,您会有更容易的时间。 –

    +1

    iam学习PHP函数 – Hafiz

    +4

    你选择了一个最复杂的东西,你最好学习一些更简单的东西。 –

    回答

    2

    这将返回第一次出现的第一个代码。修改$ content数组的数组索引以获得其他数组。

    preg_match("|[p1](.+)[/p1]|si", $original_code, $content); 
    echo $content[1]; 
    

    还与你的HTML来取代它,更换整个发生,从[P1]开始,以[/ P1]与HTML的任何功能结束。要做到这一点,你需要preg_replace()和一个正则表达式(正则表达式),它匹配里面的所有东西,包括[p1]标签。 - >

    preg_replace('#([[p1])(.*)([\p1]])#', makeHTML(), $original_code, $limit_changes); 
    

    更换代码应该是这样的:

    preg_match("|[p1](.+)[/p1]|si", $original_code, $content); 
    
    for($i=1; $i<=count($content); $i++){ 
        echo $content[$i]; //This is the code between the tags 
        preg_replace('#([[p1])(.*)([\p1]])#', makeHTML($content[$i]), $original_code, 1); 
    } 
    

    $ original_code是你需要更换一串代码。

    $内容与[P1]标签

    我假定参数到makeHTML函数应该是[P1]标签之间的代码之间的代码的阵列。

    这应该做到这一点。 ;)投票给我的答案!

    +0

    您的问题已解决。我认为。祝你好运!如果您还有其他问题,请询问。 –

    +0

    谢谢请定义这些$ text_html =? $内容=? $ your_search_subject =? – Hafiz

    +0

    $ content是包含[p1]标签之间的代码的数组。 $ text_html是具有原始html或您要替换的任何代码的变量。 $ your_search_subject应该与$ text_html相同。我忘了改名字了。 –

    相关问题