繁体   English   中英

忽略preg_match上的html标签

[英]Ignore html tags on preg_match

我用以下HTML报废网站

<a class="name" href="/link" data-hovercard-id="charshere"><span class="highlighted">War</span> World</a> 

<a class="name" href="/link" data-hovercard-id="charshere"> World of <span class="highlighted">fun</span></a> 

<a class="name" href="/link" data-hovercard-id="charshere">Save the<br>world</a> 

<a class="name" href="/link" data-hovercard-id="charshere">world of warcraft</a> 

使用此代码,我得到链接的价值

preg_match_all('/<a class="name" href=".*?" data-hovercard-id=".*?">(.*)<\/a>/i', $file_string, $titles);

但结果是

<span class="highlighted">War</span> World
 World of <span class="highlighted">fun</span>
Save the<br>world
world of warcraft

我如何忽略其中的html标签? 这样看起来像这样

 War World
 World of fun
 Save the world
 world of warcraft

一个DomDocument可能更好。 谢谢。 一直在尝试使用domDocument,但我不熟悉如何使用其xquery。

使用strip_tags() 这里有一个例子:

$html = <<<EOF
<span class="highlighted">War</span> World
 World of <span class="highlighted">fun</span>
Save the<br>world
world of warcraft
EOF;

echo strip_tags($html);

输出:

War World
 World of fun
Save theworld
world of warcraft

收到文字后,只需删除标签即可:

<?php
$string = '<span class="highlighted">War</span> World
 World of <span class="highlighted">fun</span>
Save the<br>world
world of warcraft';
$convert = preg_replace('/<.*?>/','', $string);
print $convert;

印刷品:

War World
 World of fun
Save theworld
world of warcraft

在为链接匹配字符串后,可以删除HTML标签。 例如

$str = preg_replace('/<[^<]+>/', '', $html);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM