我有一个包含以下数据的变量。
$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
由于
答案 0 :(得分: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";
}
}
答案 1 :(得分:1)
preg_match_all
this =&gt; /<[^<]+?>(.)+<[^<]+?>/
。
http://us2.php.net/manual/en/function.preg-match-all.php
将抓住标签之间的所有内容
答案 2 :(得分:0)
假设您在网页上显示此内容并且浏览器已准备就绪,您只需回显变量的值即可。不确定是否需要常量: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];
?>
可选(跳过上面的段落部分并在下面输出新的欢呼声):
<html>
<head><title>Test</title></head>
<body>
<h1>Your output</h1>
<?php echo $new_cheers; ?>
</body>
</html>
由于您已经拥有HTML标记,因此浏览器将读取格式并正确呈现它。