2012-09-17 58 views
0

我想從某個html文檔中刪除所有星號。但只有那些在ul標籤內的。其中一些ul-tags也有一個類。 經過幾個小時的嘗試,我決定問。PHP正則表達式:匹配所有ul html標籤內的所有星號

感謝您的任何幫助。

+0

也許不是你要找的答案,但還是值得一讀:HTTP:// WWW。 codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html – rodion

回答

0

你可以試試這個:

<ul[^>]*?>(.*?[*]+.*?)*</ul> 
+0

謝謝你的回答。 由於某些原因,ul內沒有任何匹配。 – mattow

+0

@mattow你可以發佈一個HTML示例代碼嗎? – Stephan

0

這裏是你需要的東西:

<?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; 

?> 
相關問題