如何使用正则表达式去除 PHP 中的标签?

发布于 2024-09-17 17:56:51 字数 743 浏览 4 评论 0原文

$string = 'text <span style="color:#f09;">text</span>
<span class="data" data-url="http://www.google.com">google.com</span>
text <span class="data" data-url="http://www.yahoo.com">yahoo.com</span> text.';

我想要做的是从所有带有 data 类的跨度中获取 data-url 。所以,它应该输出:

$string = 'text <span style="color:#f09;">text</span>
http://www.google.com text http://www.yahoo.com text.';

然后我想删除所有剩余的 html 标签

$string = strip_tags($string);

输出:

$string = 'text text http://www.google.com text http://www.yahoo.com text.';

有人可以告诉我如何做到这一点吗?

$string = 'text <span style="color:#f09;">text</span>
<span class="data" data-url="http://www.google.com">google.com</span>
text <span class="data" data-url="http://www.yahoo.com">yahoo.com</span> text.';

What I want to do is get the data-url from all spans with the class data. So, it should output:

$string = 'text <span style="color:#f09;">text</span>
http://www.google.com text http://www.yahoo.com text.';

And then I want to remove all the remaining html tags.

$string = strip_tags($string);

Output:

$string = 'text text http://www.google.com text http://www.yahoo.com text.';

Can someone please tell me how this can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幼儿园老大 2024-09-24 17:56:52

如果您的字符串不仅仅包含您显示的 HTML 片段,您应该使用 DOM< /a> 与此 XPath

//span/@data-url

示例:

$dom = new DOMDocument;
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
foreach( $xp->query('//span/@data-url') as $node ) {
    echo $node->nodeValue, PHP_EOL;
}

上面将输出

http://www.google.com
http://www.yahoo.com

当您已经加载了 HTML,您还可以

echo $dom->documentElement->textContent;

在这种情况下返回与 strip_tags($string) 相同的结果:

text text
google.com
text yahoo.com text.

If your string contains more than just the HTML snippet you show, you should use DOM with this XPath

//span/@data-url

Example:

$dom = new DOMDocument;
$dom->loadHTML($string);
$xp = new DOMXPath($dom);
foreach( $xp->query('//span/@data-url') as $node ) {
    echo $node->nodeValue, PHP_EOL;
}

The above would output

http://www.google.com
http://www.yahoo.com

When you already have the HTML loaded, you can also do

echo $dom->documentElement->textContent;

which returns the same result as strip_tags($string) in this case:

text text
google.com
text yahoo.com text.
琉璃梦幻 2024-09-24 17:56:52

尝试使用 SimpleXML 并按元素进行 foreach - 然后检查 class 属性是否有效并获取 data-url

Try to use SimpleXML and foreach by the elements - then check if class attribute is valid and grab the data-url's

度的依靠╰つ 2024-09-24 17:56:52
preg_match_all("/data/" data-url=/"([^']*)/i", $string , $urls);

您可以通过这种方式获取所有 URls a=。

您还可以使用 simplexml,如 hsz 提到的

preg_match_all("/data/" data-url=/"([^']*)/i", $string , $urls);

You can fetch all URls a=by this way.

And you can also use simplexml as hsz mentioned

锦上情书 2024-09-24 17:56:52

简短的回答是:不。周围有一个可爱的咆哮所以解释为什么用正则表达式解析 html 是一个坏主意。本质上,它可以归结为“html 不是一种常规语言,因此正则表达式不足以解析它”。你需要的是 DOM 感知的东西。

正如 @hsz 所说,如果您知道您的 html 作为 XML 进行验证,那么 SimpleXML 是一个不错的选择。更好的可能是 DOMDocument::loadHTML ,它不需要很好 -形成的html。一旦你的 html 位于 DOMDocument 对象中,你就可以很容易地提取你想要的内容。请在此处查看文档。

The short answer is: don't. There's a lovely rant somewhere around SO explaining why parsing html with regexes is a bad idea. Essentially it boils down to 'html is not a regular language so regular expressions are not adequate to parse it'. What you need is something DOM aware.

As @hsz said, SimpleXML is a good option if you know that your html validates as XML. Better might be DOMDocument::loadHTML which doesn't require well-formed html. Once your html is in a DOMDocument object then you can extract what you will very easily. Check out the docs here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文