茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1669406 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00016.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2014/10/15 5:17:23
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
s/<!--[^>]*-->//g

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:
/<!--(.|\n)*?-->/g




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>




<!--([\s\S]*?)-->

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
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|