簡體   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