简体   繁体   English

忽略preg_match上的html标签

[英]Ignore html tags on preg_match

Im scrapping a site with following html 我用以下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> 

using this code i get the value of links 使用此代码,我得到链接的价值

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

but the outcome is 但结果是

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

How do i ignore the html tags inside of it? 我如何忽略其中的html标签? so that it would look like this 这样看起来像这样

 War World
 World of fun
 Save the world
 world of warcraft

A DomDocument could be better. 一个DomDocument可能更好。 Thanks. 谢谢。 been trying to use domDocument but I not familiar how to use its xquery. 一直在尝试使用domDocument,但我不熟悉如何使用其xquery。

Use strip_tags() . 使用strip_tags() Here comes an example: 这里有一个例子:

$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);

Output: 输出:

War World
 World of fun
Save theworld
world of warcraft

Just remove the tags after you get the text: 收到文字后,只需删除标签即可:

<?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;

Prints: 印刷品:

War World
 World of fun
Save theworld
world of warcraft

You can remove the HTML tags after you've matched your string for the links. 在为链接匹配字符串后,可以删除HTML标签。 For example 例如

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

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

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