php爆炸选择多选
我想将这些 html 分为几个部分。一个
或
以及一些
以及一些
和
作为一部分。我尝试了
explode
array('
','
')
,但它导致了警告
。 explode
不支持多选。
')
,但它导致了警告
。 explode
不支持多选。那么如何才能做到完美呢?谢谢。
$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;
foreach ($text as $result) {
$arr = explode(array('<h2>','<h3>'),$result);
reset($arr);
foreach($arr as $line){
echo $line.'<hr />';
}
}
警告:第 23 行为 foreach() 提供的参数无效;
我的预期输出是:
<h2>title1</h2>
<p>something</p>
<span>something</span>
___________________________
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
___________________________
<h2>title3</h2>
<span>something</span>
___________________________
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
___________________________
I want divide these html as serval several part. One <h2>
or <h3>
with some <p>
and <span>
as one part. I tried explode
array('<h2>','<h3>')
, but it caused Warning
. the explode
not support multi choose.
So how to do it perfectly? Thanks.
$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;
foreach ($text as $result) {
$arr = explode(array('<h2>','<h3>'),$result);
reset($arr);
foreach($arr as $line){
echo $line.'<hr />';
}
}
Warning: Invalid argument supplied for foreach() on line 23;
My expected output is:
<h2>title1</h2>
<p>something</p>
<span>something</span>
___________________________
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
___________________________
<h2>title3</h2>
<span>something</span>
___________________________
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
___________________________
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
preg_split()
在不同的事情上爆炸
。您可以在此处使用正则表达式:You can use
preg_split()
toexplode
at different things. You can use RegEx here:您应该使用解析器来完成此类任务。我使用 Zend Framework,它有一个 解析器组件。否则,您可以使用普通的 PHP DOMElement。然后你可以使用 xpath 或 css 选择器查询你的 dom。示例:
编辑:鉴于您的预期输出,我的示例并不完全符合您的需求,但您仍然需要一个解析器来执行这种 HTML 操作,因为解析器将 HTML 拆分为元素,并且您可以将它们作为标记来操作,而不是作为文本。
You should use a parser for this kind of tasks. I use Zend Framework which has a parser component. Otherwise you can use plain PHP DOMElement. Then you can query your dom with xpath or css selectors. Example:
Edit: Given your expected output, my example doesn't fit exactly your needs, but you still need a parser to do that kind of HTML manipulation, because the parser splits the HTML in elements and you can manipulate them as tokens, not as text.
好的,首先,警告发送到
foreach
,而不是爆炸
。您正在尝试循环一个字符串
(在本例中为$text
)而不是一个数组
。其次,即使
$text
是数组类型并且$result
是字符串类型,您也尝试使用array
作为分隔符在explode()
调用中,但该函数希望第一个参数为输入字符串
。我建议您查看How to parse HTML with PHP? 或搜索此术语,找到许多关于如何使用 PHP 解析 HTML 的帖子。
Ok, first, the warning is addressed to the
foreach
, not theexplode
. You are trying to loop astring
($text
in this case) instead of anarray
.Second, even if
$text
would be of type array and$result
would be of type string, you are trying to use anarray
as delimiter in theexplode()
call, but that function wants the 1st parameter to be of typestring
.I'd recommend to have a look at How to parse HTML with PHP? or to search SO for this terms, to find many many posts dealing with how to parse HTML with PHP.