php regular method to remove comments: 1. Create a PHP sample file and include the comment content; 2. Remove comments through the "highlight_string(removeComment(file_get_contents('./a.php')));" method That’s it.
The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer
Use regular expressions to remove comments in PHP code
Test code
File: a.PHP
<?php /** * 加法计算 * 测试 */ // 设定$a的值 $a = 10; // 设定$b的值 $b = 5; // 加法 $c = $a + $b; # 输出结果 echo $c;
File: test.php
echo "源码:<br />"; show_source('./a.php'); echo "<hr />去除注释后:<br />"; highlight_string(removeComment(file_get_contents('./a.php'))); /** * 去除PHP代码注释 * @param string $content 代码内容 * @return string 去除注释之后的内容 */ function removeComment($content){ return preg_replace("/(\/\*.*\*\/)|(#.*?\n)|(\/\/.*?\n)/s", '', str_replace(array("\r\n", "\r"), "\n", $content)); }
Test output
Execute test .php, the output is as follows:
Regular analysis
(\/\*.*\*\/) 匹配 /* */ (#.*?\n) 匹配 # 遇到第一个回车后结束 (\/\/.*?\n) 匹配 // 遇到第一个回车后结束
The editor of this site will continue to add:
Multi-line comments
<?php $s = '/*aaaa sss */*/'; preg_match('@/\*.*?\*/@s', $s, $match); var_dump($match);
Run result
array(1) { [0]=> string(15) "/*aaaa sss */" }
Match single line comment
<?php $str = " a = b; //asdf //saf b = c; //asdf "; $reg = '@\s*//.*$@m'; $res = preg_replace($reg,'',$str); echo $res;
Output
a = b; b = c;
Another Lua version
local test = "int x; /* xx */ xx*/ int y; /* y */" function string.gfind(test, patten) local i,j = 0,0; return function() i,j = string.find(test, patten, j+1) if(i == nil) then -- end find return nil end return string.sub(test, i, j) end end for v in string.gfind(test, "/%*.-%*/") do print(v) end
Output
/* xx */
/* y */
phpstorm regular matching deletes comment lines (replaces comment lines with blank lines)
Use phpstorm to write php and javascript code, It feels good and is very comfortable to use.
I encountered a need. Sometimes when reading the source code of a framework, I want to filter (delete) the comment lines in the source code. If I delete it manually line by line, it will not be scientific enough.
Of course, I thought of the powerful regular expression (regExp regular expression). By writing a regular expression, we can find all the comment lines and replace them with empty ones, which is how we achieve the requirement.
1. ctrl F , enter the regular expression: (\/\/.*$)|(\/\*(.|\s)*?\*\/)
2. ctrl R, do not enter:
3. Click Replace all
but found that this did not fully realize our needs, but only replaced the comment line with a blank line.
So, let’s write another regular expression to match the blank lines and delete them.
1.ctrl F, enter the regular expression: ^\n
2.ctrl R, do not enter
3. Click Replace all
But I found that not all blank lines were deleted. (There are still some blank lines that have not been deleted)
So, let’s continue with a regular matching deletion.
1.ctrl F, enter the regular expression: \s\n
2.ctrl R, do not enter
3. Click Replace all
Okay, here, delete all blank lines. Have fun -:)
Actually, these three steps can be implemented in one step, you can think about it! [Recommended learning: "PHP Video Tutorial"]
The above is the detailed content of How to remove comments from php regular expression. For more information, please follow other related articles on the PHP Chinese website!