次のデータを含む変数があります。
$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
ありがとう
文字列の解析を検討している場合、これでうまくいくはずです。
<?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";
}
}
preg_match_all
これ => /<[^<]+?>(.)+<[^<]+?>/
.
http://us2.php.net/manual/en/function.preg-match-all.php
は、タグ間のすべてを取得します
これを 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 タグがあるため、ブラウザーはフォーマットを読み取り、適切にレンダリングします。