#','',string)"."/> #','',string)".">
Home >Backend Development >PHP Problem >How to delete html comments in php
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)".
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 = '<!--ceshi-->ceshi'; $a = preg_replace('#<!--.*-->#' , '' , $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 = '<!--ceshi-->ceshi<!--ceshi-->'; $a = preg_replace('#<!--.*-->#' , '' , $a); var_dump($a);
So we changed the matching rules to the following format
preg_replace('#<!--.*?-->#' , '' , $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('#<!--[^\!\[]*?(?<!\/\/)-->#' , '' , $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!