我想從某個html文檔中刪除所有星號。但只有那些在ul標籤內的。其中一些ul-tags也有一個類。 經過幾個小時的嘗試,我決定問。PHP正則表達式:匹配所有ul html標籤內的所有星號
感謝您的任何幫助。
我想從某個html文檔中刪除所有星號。但只有那些在ul標籤內的。其中一些ul-tags也有一個類。 經過幾個小時的嘗試,我決定問。PHP正則表達式:匹配所有ul html標籤內的所有星號
感謝您的任何幫助。
這裏是你需要的東西:
<?php
// a function to remove any asterisks from a string
function replace_asterisks($replace){
return str_replace('*', '', $replace[0]);
}
$html = '* blah blah * * <ul>blah<li>*</li>*</ul> sad sad <ul>**</ul>';
// if found any <ul> with asterisks, pas the part to that function to remove asterisks from it
$html = preg_replace_callback('/<ul.*?>.*?\*.*?.*?<\/ul>/', 'replace_asterisks', $html);
echo $html;
?>
也許不是你要找的答案,但還是值得一讀:HTTP:// WWW。 codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html – rodion