测试文件代码如下:
<?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(" ", " "), " ", $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("
注意:代码中有//的,都会去除