How to remove all comments from web page files in php

*文
Release: 2023-03-18 22:32:01
Original
2176 people have browsed it

php如何去除网页文件中所有的注释?下面小编就为大家带来一篇php正则去除网页中所有的html,js,css,注释的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望对大家有所帮助。

如下所示:

$search = array ("&#39;<script[^>]*?>.*?</script>&#39;si", // 去掉 javascript
 "&#39;<style[^>]*?>.*?</style>&#39;si",  // 去掉 css
 "&#39;<[/!]*?[^<>]*?>&#39;si",      // 去掉 HTML 标记
 "&#39;<!--[/!]*?[^<>]*?>&#39;si",      // 去掉 注释 标记
 "&#39;([rn])[s]+&#39;",  // 去掉空白字符
 "&#39;&(quot|#34);&#39;i",  // 替换 HTML 实体

 "&#39;&(amp|#38);&#39;i",
 "&#39;&(lt|#60);&#39;i",
 "&#39;&(gt|#62);&#39;i",
 "&#39;&(nbsp|#160);&#39;i",
 "&#39;&(iexcl|#161);&#39;i",
 "&#39;&(cent|#162);&#39;i",
 "&#39;&(pound|#163);&#39;i",
 "&#39;&(copy|#169);&#39;i",
 "&#39;(d+);&#39;e");   // 作为 PHP 代码运行
 
$replace = array ("",
 "",
 "",
 "",
 "\1",
 "\"",
 "&",
 "<", 
 ">",
 " ",
 chr(161),
 chr(162),
 chr(163),
 chr(169),
 "chr(\1)");
//$document为需要处理字符串,如果来源为文件可以$document = file_get_contents($filename);
$out = preg_replace($search, $replace, $document);
Copy after login

也可以使用php的内置函数strip_tags()清除html,js,注释等标记

相关推荐:

php捕捉特定类型的异常详解

PHP如何处理MySQL死连接

php项目中使用emoji表情的解决方法

The above is the detailed content of How to remove all comments from web page files in php. 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!