Home >Backend Development >PHP Problem >How to remove html comments
You can use the preg_replace function to delete html comments, the syntax format is "preg_replace (pattern to be searched, "", string or array)". preg_replace performs a regular expression search and replace.
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer.
The content of the comments in the html can be deleted using regular expressions.
1. The general html comment writing method is f80737884d6c93a4ecaeac0ca41583ac
b7eed15494c5e94925ac29cde8f7bece
2. You can use the following custom method to delete useless comments:
function remove_html_comments($content = "") { return preg_replace("/<!--(.|\s)*?-->/", "", $content); }
The input parameter is content, preg_replace is regular replacement, and the rule is 74c7351874ca4efbaf4174d3c6ce4966
Extended information:
preg_replace — Perform a regular expression search and replace
preg_replace ( string|array $pattern , string|array $replacement , string|array $subject , int $limit = -1 , int &$count = null )
search The part of subject that matches pattern is replaced with replacement.
Parameter description:
$pattern: The pattern to be searched, which can be a string or a string array.
$replacement: String or array of strings used for replacement.
$subject: The target string or string array to be searched and replaced.
$limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).
$count: Optional, the number of times the replacement is performed.
Return value
If subject is an array, preg_replace() returns an array, otherwise it returns a string.
If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.
Recommended learning: php video tutorial
The above is the detailed content of How to remove html comments. For more information, please follow other related articles on the PHP Chinese website!