|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1621420
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2014/10/15 5:17 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15766
|
- [分享]使用正規表示式(RegExp)來處裡 HTML 註解(comments)
- RegExp to strip HTML comments
Looking for a regexp sequence of matches and replaces (preferably PHP but doesn't matter) to change this (the start and end is just random text that needs to be preserved).
IN:
fkdshfks khh fdsfsk
<!--g1-->
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<!--eg1-->
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
<!--gc2-->
<!--bXNnYm94-->
<!--egc2-->
<!--g2-->
</div>
<!--eg2-->
fdsfdskh
to this OUT:
fkdshfks khh fdsfsk
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
</div>
fdsfdskh
Thanks.
Are you just trying to remove the comments? How about or the slightly better (suggested by the questioner himself): But remember, HTML is not regular, so using regular expressions to parse it will lead you into a world of hurt when somebody throws bizarre edge cases at it.
preg_replace('/<!--(.*)-->/Uis', '', $html)
This PHP code will remove all html comment tags from the $html string.
Do not forget to consider conditional comments, as will remove them. Try this instead: This will also remove downlevel-revealed conditional comments, though.
EDIT: This won't remove downlevel-revealed or downlevel-hidden comments.
Ah I've done it,
Try the following if your comments contain line breaks:
these code is also remove javascript code. that's too bad :| here's the example javascript code will be remove with this code:
<script type="text/javascript"><!--
var xxx = 'a';
//-->
</script>
Works in javascript and VBScript also as "." doesn't match line breaks in all languages share|improve this answer
function remove_html_comments($html) {
$expr = '/<!--[\s\S]*?-->/';
$func = 'rhc';
$html = preg_replace_callback($expr, $func, $html);
return $html;
}
function rhc($search) {
list($l) = $search;
if (mb_eregi("\[if",$l) || mb_eregi("\[endif",$l) ) {
return $l;
}
}
冷日表示: 如何使用正規表示式真的可以很便利的幫你處理檔案,大量節省你的時間! 特別是在處理別人家的 HTML 的時候,不管是 TAG 或是註解,正規表示式都是好物阿! 一般來說下面這樣就夠用了,但是其他的資料還是可以參考參考喔!
原文出處:php - RegExp to strip HTML comments - Stack Overflow
|
|
|
|