#','',string)"."/> #','',string)".">

Home >Backend Development >PHP Problem >How to delete html comments in php

How to delete html comments in php

青灯夜游
青灯夜游Original
2021-06-02 19:28:372214browse

In PHP, you can use the preg_replace() function with regular expressions to delete html comments. This function can perform a regular expression search and replacement; the specific syntax format is "preg_replace('#4f6298c8fdc1b7cf278534e8b7c135ea#','',string)".

How to delete html comments in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

How to delete html comments in php

The first thing that is more basic is:

    $a = &#39;<!--ceshi-->ceshi&#39;;
    $a = preg_replace(&#39;#<!--.*-->#&#39; , &#39;&#39; , $a);
    var_dump($a);

The above code will output ceshi.

But if it is the following string, it cannot achieve the effect we want

    $a = &#39;<!--ceshi-->ceshi<!--ceshi-->&#39;;
    $a = preg_replace(&#39;#<!--.*-->#&#39; , &#39;&#39; , $a);
    var_dump($a);

So we changed the matching rules to the following format

preg_replace(&#39;#<!--.*?-->#&#39; , &#39;&#39; , $a);

But in If there is code like 6fc79538f41ba7309e847292db36ab30ceshi1b771f47d72d900ba74308aee59557f0 in html, it cannot be removed, so we need to improve the matching rules and change Into the following format

preg_replace('#dde0efc6a8975ca86d748aeecb1cb5d4#' , '' , $a);

and then if there is 3f1c4e4b6b16bbbd69b2ee476dc4f83a7aebe67afabe52797c753f913db6d8a62cacc6d41bbb37262a98f745aa00fbf0 code in the html, we need Let’s change our matching rules to the following format

preg_replace(&#39;#<!--[^\!\[]*?(?<!\/\/)-->#&#39; , &#39;&#39; , $a);

In this case, I will basically remove the html comments that I need to remove!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete html comments in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn