簡體   English   中英

html標簽之間的preg_match

[英]preg_match between html tags

我需要抓住html標記之間的名稱。

<div class="from"><span class="profile fn">firstnamed familyname</span></div>

到目前為止,我根據其他人的例子嘗試了同樣的問題:

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

但這不起作用。

正確的方法是什么?

非常感謝。

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

...應觸發此操作:

解析錯誤:語法錯誤,意外的“ <”

除此之外:

  • 您要尋找原始文本中沒有的未封閉屬性:

    class="profile fn vs class="profile fn"

  • 您尋找零個或一個字符:

    .?

固定的正則表達式為:

$text = '<div class="from"><span class="profile fn">firstnamed familyname</span></div>';
preg_match(';from"><span class="profile fn">(.*)</span></div>;', $text, $match);
var_dump($match);

當然,這可能會在大型HTML文檔上中斷(稍后會再出現</span></div> )。 當用於解析HTML時,正則表達式是不可能正確的。

這個:

preg_match(";from"><span class="profile fn>(.?)</span></div>;", $text, $match)

在語法上是不正確的,您必須轉義雙引號:

preg_match(";from\"><span class=\"profile fn>(.?)</span></div>;", $text, $match)

您需要轉義特殊字符(例如引號):

preg_match(";from\"><span class\=\"profile fn>(.?)</span></div>;", $text, $match)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM