How to remove html comments

醉折花枝作酒筹
Release: 2023-03-10 12:30:01
Original
2353 people have browsed it

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.

How to remove html comments

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

2. You can use the following custom method to delete useless comments:

function remove_html_comments($content = "") {
        return preg_replace("/<!--(.|\s)*?-->/", "", $content);
      }
Copy after login

The input parameter is content, preg_replace is regular replacement, and the rule is

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 )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!