查找所有 <pre>PHP 中的标签(带属性) [英] Find all <pre> tags in PHP (with attributes)
问题描述
我正在关注这个问题 关于如何在 PHP 中检索所有标签.
特别是(在 wordpress 下),我想找到所有
标签,以及所有可用的信息(属性和文字).但是,我似乎不太擅长preg_match,所以我向您求助.我的文本确实包含各种
<pre>
标签,一些带有属性,一些带有文本.我的功能是这样的:function getPreTags($string) {$pattern = "/
(.*)<\/pre>/";preg_match($pattern, $string, $matches);返回 $matches[1];}我已经简化为只有 one
标签的测试,但我得到count(getPreTags(myHTMLbody)) = 0代码>,我不知道为什么.这是测试字符串:
==<pre class="wp-code-highlight Prettyprint Prettyprinted" style=""><span class="com">随便<</span>我已经写了 >>这里应该注意<span class="something"></span></pre>
有什么提示吗?
干杯!
解决方案一如既往,使用正则表达式解析 HTML 永远不会削减它.有很多事情要考虑(标签汤,间距:
==
<\n\t\sPrE\n\n>
...),任何正则表达式都会在某些时候使您失败.这就是为什么有解析器之类的东西,随时可用.也就是说:当你需要 all
pre
时,我不知道为什么其他答案会遇到使用DOMXPath
实例的麻烦> 标签,包括没有属性的标签.
我会选择更简单的东西,例如:$dom = 新的 DOMDocument;$dom->loadHTML($htmlString);$preTags = $dom->getElementsByTagName('pre');foreach($preTags as $pre){echo $pre->nodeValue, PHP_EOL;if ($pre->hasAttributes()){//如果有属性foreach($pre->attributes as $attribute){//用属性做一些事情echo '属性:', $attribute->name, '=', $attribute->value, PHP_EOL;}}}
可以在这些页面上轻松找到哪些方法和属性可供您使用:
I was following this question on how to retrieve all tags in PHP.
Specifically (under wordpress), I'd like to find all <pre>
tags, with all the available information (attributes and text). However, it seems that I'm not that skilled in preg_match, so I'm turning to you.
My text does contain various <pre>
tags, some with attributes, some with just text. My function is this:
function getPreTags($string) {
$pattern = "/<pre\s?(.*)>(.*)<\/pre>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
I've reduced to a test with just one <pre>
tag, but I get count(getPreTags(myHTMLbody)) = 0
, and I don't know why. This is the test string:
<pre class="wp-code-highlight prettyprint prettyprinted" style=""><span class="com">Whatever <</span> I've written >> here <span class="something">should be taken care of</span></pre>
Any hint?
Cheers!
As ever, parsing HTML with regex is never going to cut it. There are so many things to take into account (tag-soup, spacing: <pre>
==< pre >
==<\n\t\sPrE\n\n>
...), any regex will fail you at some point. That's why there are such things as parsers, readily available.
That said: I have no idea why the other answers go through the trouble of using an instance of DOMXPath
, when you need all pre
tags, including those without attributes.
I'd go for something more simple, like:
$dom = new DOMDocument;
$dom->loadHTML($htmlString);
$preTags = $dom->getElementsByTagName('pre');
foreach($preTags as $pre)
{
echo $pre->nodeValue, PHP_EOL;
if ($pre->hasAttributes())
{//if there are attributes
foreach($pre->attributes as $attribute)
{
//do something with attribute
echo 'Attribute: ', $attribute->name, ' = ', $attribute->value, PHP_EOL;
}
}
}
What methods and properties are available to you can be found easily on these pages:
- Attributes:
DOMAttr
class docs - Nodes:
DOMNode
class docs - Document:
DOMDocument
class docs
这篇关于查找所有 <pre>PHP 中的标签(带属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!