Вопрос по html, php, regex – RegExp, чтобы убрать комментарии HTML

35

Поиск регулярного выражения соответствует и заменяет (предпочтительно PHP, но это не имеет значения), чтобы изменить это (начало и конец - это просто случайный текст, который необходимо сохранить).

В:

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

к этому 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

Благодарю.

Ваш Ответ

11   ответов
2
<!--([\s\S]*?)-->

48
preg_replace('/<!--(.*)-->/Uis', '', $html)

Error: User Rate Limit Exceeded<!--Error: User Rate Limit Exceeded<script> //... <!--</script> <script type="text/javascript" src="http://.."></script> </head> <!-- body -->
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1
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;
   }
}
0
// Remove multiline comment
    $mlcomment = '/\/\*(?!-)[\x00-\xff]*?\*\//';
    $code = preg_replace ($mlcomment, "", $code);
// Remove single line comment
    $slcomment = '/[^:]\/\/.*/';
    $code = preg_replace ($slcomment, "", $code);
// Remove extra spaces
    $extra_space = '/\s+/';
    $code = preg_replace ($extra_space, " ", $code);
// Remove spaces that can be removed
    $removable_space = '/\s?([\{\};\=\(\)\\\/\+\*-])\s?/';
    $code = preg_replace ('/\s?([\{\};\=\(\)\/\+\*-])\s?/', "\\1", $code);
17

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

<!--
multi line html comment
-->

Error: User Rate Limit Exceeded

<!-- single line html comment -->

Error: User Rate Limit Exceededwhat is most importantError: User Rate Limit Exceeded

<!-- this is my blog: <mynixworld.inf> -->

Note

Error: User Rate Limit Exceeded

<!--[if !(IE 8) ]><!-->
Error: User Rate Limit Exceeded(?=<!--)([\s\S]*?-->)
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded(<!--[\s\S]*?-->) ?
1

<script type="text/javascript"><!--
    var xxx = 'a';
    //-->
    </script>
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded/(?:[^>]\s|^)<!--(?!<!)[^\[>][\s\S]*?-->/g jsfiddle.net/3vkrrt8e
Error: User Rate Limit Exceeded
2

Error: User Rate Limit Exceeded

/<!--(.|\n)*?-->/g
Error: User Rate Limit ExceededsError: User Rate Limit Exceeded.Error: User Rate Limit ExceededsError: User Rate Limit Exceeded.Error: User Rate Limit Exceeded[\S\s]Error: User Rate Limit Exceeded(.|\n)Error: User Rate Limit ExceededgError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1

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

8

Error: User Rate Limit Exceeded

<!--(.*?)-->
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
73

Error: User Rate Limit Exceeded

s/<!--[^>]*-->//g

Error: User Rate Limit Exceeded

<!--(.*?)-->

Error: User Rate Limit ExceedednotError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded James Brooks
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
16

Error: User Rate Limit Exceeded

<!--(.*?)-->

Error: User Rate Limit Exceeded

<!--[^\[](.*?)-->

Error: User Rate Limit Exceeded

EDIT:

Error: User Rate Limit Exceeded

<!--(?!<!)[^\[>].*?-->

Похожие вопросы