php
  • html
  • function
  • 2011-08-20 158 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相同。我忘了改名字了。 –

    相關問題