-3

次のデータを含む変数があります。

$cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p> " ;

以下の表示のような出力を取得する方法:

This is apple
This is school bus
This is php code

ありがとう

4

3 に答える 3

1

文字列の解析を検討している場合、これでうまくいくはずです。

<?php
   $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p>";
   $cheers = strip_tags($cheers);
   $arrtext = explode("content:op=", $cheers);

   foreach ($arrtext as $element){
      if ($element!=""){
            echo $element."\n\r";
       }
    }
于 2012-07-13T04:04:45.257 に答える
1

preg_match_allこれ => /<[^<]+?>(.)+<[^<]+?>/. http://us2.php.net/manual/en/function.preg-match-all.php は、タグ間のすべてを取得します

于 2012-07-13T04:53:04.710 に答える
0

これを Web ページに表示していて、ブラウザーで準備ができていると仮定すると、変数の値をエコーするだけです。必要と思われない定数:op 部分が必要かどうかはわかりません。以下のように書き直すことができます。

    <?php
    // define variable
    $cheers="<p>content:op=This is an apple</p> <p>content:op=This is a school bus</p> <p>content:op=This is PHP code</p> " ;

    // remove extra text
    $new_cheers = str_replace("content:op=","",$cheers); 

    // split paragraphs (if needed, otherwise just echo $new_cheers)
    $paragraphs = explode(" ", $new_cheers); 
    echo $paragraphs[0]; 
    echo $paragraphs[1]; 
    echo $paragraphs[2];
    ?>

オプション (上記の $paragraphs 部分をスキップし、下に新しい歓声を出力するだけです):

<html>
<head><title>Test</title></head>
<body>
<h1>Your output</h1>
<?php echo $new_cheers; ?>
</body>
</html>

既に HTML タグがあるため、ブラウザーはフォーマットを読み取り、適切にレンダリングします。

于 2012-07-13T03:18:37.550 に答える