Home > Backend Development > PHP Problem > How to remove comments from php regular expression

How to remove comments from php regular expression

藏色散人
Release: 2023-03-08 09:30:01
Original
1677 people have browsed it

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.

How to remove comments from php regular expression

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;
Copy after login

File: test.php

echo "源码:<br />";
show_source(&#39;./a.php&#39;);
echo "<hr />去除注释后:<br />";
highlight_string(removeComment(file_get_contents(&#39;./a.php&#39;)));
 
/**
 * 去除PHP代码注释
 * @param string $content 代码内容
 * @return string 去除注释之后的内容
 */
function removeComment($content){
 return preg_replace("/(\/\*.*\*\/)|(#.*?\n)|(\/\/.*?\n)/s", &#39;&#39;, str_replace(array("\r\n", "\r"), "\n", $content));
}
Copy after login

Test output

Execute test .php, the output is as follows:

How to remove comments from php regular expression

Regular analysis

(\/\*.*\*\/) 匹配 /* */
(#.*?\n)  匹配 #  遇到第一个回车后结束
(\/\/.*?\n) 匹配 //  遇到第一个回车后结束
Copy after login

The editor of this site will continue to add:

Multi-line comments

<?php
  
$s = &#39;/*aaaa
sss
*/*/&#39;;
  
preg_match(&#39;@/\*.*?\*/@s&#39;, $s, $match);
  
var_dump($match);
Copy after login

Run result

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

Match single line comment

<?php
$str = " 
a = b; //asdf //saf
b = c; //asdf
  ";
$reg = &#39;@\s*//.*$@m&#39;;
  
$res = preg_replace($reg,&#39;&#39;,$str);
  
echo $res;
Copy after login

Output

a = b;
b = c;
Copy after login

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
Copy after login

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!

Related labels:
php
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