php
  • html
  • function
  • 2011-08-20 9 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を作るが、2番目のために任意のヘルプを作成する方法やアイディア ?

    +1

    HTMLパーサを使用した方がはるかに簡単です。 –

    +1

    iam PHP関数の学習 – Hafiz

    +4

    あなたは最も複雑なものの1つを選択しました。 –

    答えて

    2

    これは、最初のオカレンス内の最初のコードを返します。 $ content配列の配列インデックスを変更して他の配列を取得します。

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

    また[P1]からスタートし、HTMLどんな機能を[/ P1]で終わる、あなたのhtmlに置き換える全体の発生を交換します。これを行うには、[p1]タグを含む内部すべてに一致するpreg_replace()と正規表現(正規表現)が必要です。 - >交換用のコードは次のようになります

    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はあなたが交換する必要があるコードの文字列です。

    $コンテンツ IはmakeHTML関数へのパラメータは、[P1]タグの間のコードでなければならないと仮定[P1]タグの間にコード

    の配列です。

    それはそれを行う必要があります。 ;)私の答えに投票してください!

    +0

    問題は解決しました。おもう。がんばろう!これ以上質問がある場合は、尋ねてください。 –

    +0

    これらの$ text_html =をご指定いただきありがとうございます。 $ content =? $ your_search_subject =? – Hafiz

    +0

    $ contentは、[p1]タグ間のコードを含む配列です。 $ text_htmlは元のhtmlや置き換えているコードの変数です。 $ your_search_subjectは$ text_htmlと同じでなければなりません。私は名前を変えるのを忘れた。 –

    関連する問題