Home>Article>Backend Development> How to remove comments through php regular expression

How to remove comments through php regular expression

藏色散人
藏色散人 Original
2020-10-07 01:04:04 2195browse

How to implement regular comment removal in php: first create a PHP sample file; then use the regular expression "reg_replace("/(\/\*.*\*\/)|(#.*?\n )|(\/\/.*?\n)/s", '', str_replace((...))" can be removed.

How to remove comments through php regular expression

Recommended: "PHP Video Tutorial"

Test Code

File: a.PHP

/**
* 加法计算
* 测试
*/
// 设定$a的值
$a = 10;
// 设定$b的值
$b = 5;
// 加法
$c = $a + $b;
# 输出结果
echo $c;

File: test.php

echo "源码:
";
show_source('./a.php');
echo "
去除注释后:
";
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) 匹配 // 遇到第一个回车后结束

Continue to add:

Multi-line comments

 
$s = '/*aaaa
sss
*/*/';

preg_match('@/\*.*?\*/@s', $s, $match);

var_dump($match);

Run Result

array(1) {
[0]=>
string(15) "/*aaaa
sss
*/"
}

Match single line comment

$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)

It feels good to use phpstorm to write php and javascript code. It is also very comfortable to use.

I encountered a need. Sometimes when reading the source code of the 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 achieves 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 found that not all blank lines were deleted. (There are still some blank lines The line is not deleted)

So, let’s continue with a regular match to delete.

1.ctrl F, enter the regular expression:\s\n

2.ctrl R, do not enter

3. Click Replace all

Okay, here, delete all the blank lines. Have fun -:)

Actually, these three steps can be implemented in one step, you can think about it!

The above is the detailed content of How to remove comments through php regular expression. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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