我有一个包含以下数据的变量。
$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
将抓取标签之间的所有内容
假设您在网页上显示它并且它已由浏览器准备好,您只需回显变量的值。不确定您是否需要似乎没有必要的常量:操作部分。它可以像下面这样重写。
<?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 标签,因此浏览器将读取格式并正确呈现。