Php 如果页面有超过1个段落,则回显消息
我有这样的代码:Php 如果页面有超过1个段落,则回显消息,php,Php,我有这样的代码: <div id="content"> ...some included PHP files here </div> if <div id="content"> contains more then 1 paragraph, then echo "more then one" after paragraph 1. …此处包含一些PHP文件 我需要一个PHP解决方案,如下所示: <div id="content"> ...s
<div id="content">
...some included PHP files here
</div>
if <div id="content"> contains more then 1 paragraph,
then echo "more then one" after paragraph 1.
…此处包含一些PHP文件
我需要一个PHP解决方案,如下所示:
<div id="content">
...some included PHP files here
</div>
if <div id="content"> contains more then 1 paragraph,
then echo "more then one" after paragraph 1.
如果包含超过1个段落,
然后在第1段后重复“不止一个”。
我需要的是PHP而不是jQuery。可能吗
如果无法处理段落,那么如果回音出现在“x”字数或“y”字符数之后也可以
PHP中有一个
str\u word\u count
函数,但我不知道如何获取包含PHP的div的内容。您可以使用输出缓冲
做一些类似于:
ob_start();
?>
<div></div>
$html = ob_get_clean();
ob_start();
?>
$html=ob_get_clean();
然后,计算$html变量内的段落数,并输出或不输出字符串。若要从标记中获取值并将其放入变量,可以尝试以下操作-
$html='1!'
';
$doc=新的DOMDocument();
$doc->loadHTML($html);
$liList=$doc->getElementsByTagName('li');
$liValues=array();
foreach($liList作为$li){
$liValues[]=$li->nodeValue;
}
var_dump($liValues);
Manoj Yadav的学分使用ob系统<代码>ob_start()。。。在这里建立你的html$html=ob_get_clean(),然后将该html馈送到DOM并进行div/段落发现。按照@MarcB的建议将html放入变量后,您可以使用PHP DOMDocument对象进行html遍历。这就是我需要的。感谢您实际发布了一个解决方案,而不仅仅是一条评论。如果以后删除,最好包含“其他答案”中的代码。不鼓励仅链接的答案。