php去掉注释的方法:【function removecomment($content){return preg_replace(/(\/\*(\s|.)*?\*\/)|(\/\/.(\s|.*))|(#(\s*)?(.*))...}】。
本文操作环境:windows10系统、php 7、thinkpad t480电脑。
假如我们要去除代码中的注释,那么可以使用正则表达式来实现。我们一起来看看具体是如何实现的。
先给出一段测试代码:
<?php /** * created by phpstorm. * user: yang * date: 2019/10/16 * time: 10:25 */ // 计算和// 计算和// 计算和$a = 1;$b = 2;$c = $a+$b; //总和 /* * 求和函数 */function sum($a, $b) { return $a + $b; //返回值} # 第二种注释$a = 1;$b = 2;## 求乘积$c = $a * $b; # 结果 //特殊$usedfuncs = "abcd";preg_split("//is", implode("", $usedfuncs), -1, preg_split_no_empty);
去除注释代码如下:
/** * created by phpstorm. * user: 25754 * date: 2019/10/17 * time: 9:54 */ function removecomment($content){ return preg_replace("/(\/\*(\s|.)*?\*\/)|(\/\/.(\s|.*))|(#(\s*)?(.*))/", '', str_replace(array("\r\n", "\r"), "\n", $content));} $content = file_get_contents("./test.php");echo removecomment($content);
结果:
$a = 1;$b = 2;$c = $a+$b; function sum($a, $b) { return $a + $b; } $a = 1;$b = 2; $c = $a * $b; $usedfuncs = "abcd";preg_split("
推荐学习:php培训
以上就是php如何去掉注释的详细内容。