[英]use regular expression to select part of html tag from string in php
我想在php的div標簽中選擇字符串。 我打算用正則表達式來做。 但是有更好的方法嗎?
像這樣: Hello there <div>how are you?</div>. Please <div>send my regards</div> to Allan.
Hello there <div>how are you?</div>. Please <div>send my regards</div> to Allan.
我想檢索所有文本以及div標簽,例如:
"<div>how are you?</div>"
"<div>send my regards</div>"
我可以知道怎么做嗎?
我打算用正則表達式來做。
但是有更好的方法嗎?
DOM Parser
。 $html='Hello there <div>how are you?</div>. Please <div>send my regards</div> to Allan.';
$dom = new DOMDocument;
$dom->loadHTML($html);
$divText='';
foreach ($dom->getElementsByTagName('div') as $tag) {
$divText.=$dom->saveHTML($tag);
}
echo $divText;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.