$ str ='一些文字標籤內容更多文字';如何使用正則表達式去除標籤及其內容?
我的問題是: 如何檢索內容tag <em>contents </em>
這是介於<MY_TAG> .. </MY_TAG>
之間?
而且
如何從$str
<MY_TAG>
及其內容?
我正在使用PHP。
謝謝。
$ str ='一些文字標籤內容更多文字';如何使用正則表達式去除標籤及其內容?
我的問題是: 如何檢索內容tag <em>contents </em>
這是介於<MY_TAG> .. </MY_TAG>
之間?
而且
如何從$str
<MY_TAG>
及其內容?
我正在使用PHP。
謝謝。
如果MY_TAG
不能嵌套,試試這個讓比賽:
preg_match_all('/<MY_TAG>(.*?)<\/MY_TAG>/s', $str, $matches)
,並將它們刪除,請使用preg_replace
代替。
hii .. whats/s for ??謝謝你的回答 – user187580 2010-03-04 18:27:24
@ user187580:* s *標誌使'.'匹配行中斷。請參閱http://php.net/manual/en/reference.pcre.pattern.modifiers.php – Gumbo 2010-03-04 18:33:34
如果您不止一次在字符串中找到此標記,那麼您最好設置該模式的不確定性。否則,你會發現,你轉換這個字符串 「這是一個
雖然要做到這一點唯一完全正確的方法是不使用正則表達式,你可以得到你想要的東西,如果你接受它不能處理所有的特殊情況:
preg_match("/<em[^>]*?>.*?</em>/i", $str, $match);
// Use this only if you aren't worried about nested tags.
// It will handle tags with attributes
而且
preg_replace(""/<MY_TAG[^>]*?>.*?</MY_TAG>/i", "", $str);
你不想爲此使用正則表達式。一個更好的解決辦法是將內容加載到DOMDocument並使用DOM樹和標準DOM方法上它的工作:
$document = new DOMDocument();
$document->loadXML('<root/>');
$document->documentElement->appendChild(
$document->createFragment($myTextWithTags));
$MY_TAGs = $document->getElementsByTagName('MY_TAG');
foreach($MY_TAGs as $MY_TAG)
{
$xmlContent = $document->saveXML($MY_TAG);
/* work on $xmlContent here */
/* as a further example: */
$ems = $MY_TAG->getElementsByTagName('em');
foreach($ems as $em)
{
$emphazisedText = $em->nodeValue;
/* do your operations here */
}
}
對於去除最後我只是用這樣的:
$str = preg_replace('~<MY_TAG(.*?)</MY_TAG>~Usi', "", $str);
使用〜而不是/由於末尾標記中的反斜槓解決了分隔符解決的錯誤,即使轉義,這似乎也是一個問題。從開始標籤中消除>允許屬性或其他字符,並仍然獲取標籤及其所有內容。
這隻適用於嵌套不重要的情況。
Usi
修飾符表示U = Ungreedy,s =包含換行符,i =不區分大小寫。
好工作(y)工作正常。g $ ptitle = preg_replace('〜〜Usi',「」,$ ptitleWithSpan); – 2017-01-05 16:51:49
我不知道有多少次以下答案是在任何一天連接:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454# 1732454 – Nicole 2010-03-04 18:22:18
HTML語法分析器等等等等......你知道演練。 – 2010-03-04 18:22:19