完美取得代码中的注释
时间:2009-04-16
来源:互联网
本帖最后由 liexusong 于 2009-4-16 19:12 编辑
看到很多人都问怎么去掉注释,所以发个取得代码中的注释, 算法比较简单,而且准确率高!
[php]
function get_comments($code) {
$is_qutoe_one = false;
$is_qutoe_two = false;
$is_block_comment = false;
$is_line_comment = false;
$comments = array();
$len = strlen($code);
$comment = '';
for($i = 0; $i < $len; $i++) {
if($code{$i} == '"') {
$is_qutoe_one = $is_qutoe_one ? false : true;
}
if($code{$i} == "'") {
$is_qutoe_two = $is_qutoe_two ? false : true;
}
if(isset($code{$i + 1}) && $code{$i}.$code{$i + 1} == '//' && !$is_qutoe_two && !$is_qutoe_one && !$is_block_comment) {
$is_line_comment = true;
}
if(isset($code{$i + 1}) && $code{$i}.$code{$i + 1} == '/*' && !$is_qutoe_two && !$is_qutoe_one && !$is_line_comment) {
$is_block_comment = true;
}
if($is_line_comment) {
if($code{$i} != "\n" && $code{$i} != "\r") $comment .= $code{$i};
if($code{$i} == "\n" || $code{$i} == "\r" || $i == $len - 1) {
$is_line_comment = false;
$comments[] = $comment;
$comment = '';
}
}
if($is_block_comment) {
if($code{$i - 1}.$code{$i} != '*/') $comment .= $code{$i};
if($code{$i - 1}.$code{$i} == '*/') {
$is_block_comment = false;
$comments[] = $comment.$code{$i};
$comment = '';
}
}
}
return($comments);
}
$php = <<<PHP
\$var = "Hello World";
echo "Hello World";//this is comment
\$var2 = "http://www.google.cn";//my comment too
//this is comment else
//this is comment too//*this is block comment*/
/*this is block comment*/
\$string = 'http://www.baidu.com';//this is baidu
/**
I am block comment too
**/
/*this is block comment*/
PHP;
print_r(get_comments($php));
[/php]
看到很多人都问怎么去掉注释,所以发个取得代码中的注释, 算法比较简单,而且准确率高!
[php]
function get_comments($code) {
$is_qutoe_one = false;
$is_qutoe_two = false;
$is_block_comment = false;
$is_line_comment = false;
$comments = array();
$len = strlen($code);
$comment = '';
for($i = 0; $i < $len; $i++) {
if($code{$i} == '"') {
$is_qutoe_one = $is_qutoe_one ? false : true;
}
if($code{$i} == "'") {
$is_qutoe_two = $is_qutoe_two ? false : true;
}
if(isset($code{$i + 1}) && $code{$i}.$code{$i + 1} == '//' && !$is_qutoe_two && !$is_qutoe_one && !$is_block_comment) {
$is_line_comment = true;
}
if(isset($code{$i + 1}) && $code{$i}.$code{$i + 1} == '/*' && !$is_qutoe_two && !$is_qutoe_one && !$is_line_comment) {
$is_block_comment = true;
}
if($is_line_comment) {
if($code{$i} != "\n" && $code{$i} != "\r") $comment .= $code{$i};
if($code{$i} == "\n" || $code{$i} == "\r" || $i == $len - 1) {
$is_line_comment = false;
$comments[] = $comment;
$comment = '';
}
}
if($is_block_comment) {
if($code{$i - 1}.$code{$i} != '*/') $comment .= $code{$i};
if($code{$i - 1}.$code{$i} == '*/') {
$is_block_comment = false;
$comments[] = $comment.$code{$i};
$comment = '';
}
}
}
return($comments);
}
$php = <<<PHP
\$var = "Hello World";
echo "Hello World";//this is comment
\$var2 = "http://www.google.cn";//my comment too
//this is comment else
//this is comment too//*this is block comment*/
/*this is block comment*/
\$string = 'http://www.baidu.com';//this is baidu
/**
I am block comment too
**/
/*this is block comment*/
PHP;
print_r(get_comments($php));
[/php]
作者: liexusong 发布时间: 2009-04-16
不用这么麻烦.去看看Tokenizer Functions
作者: TankMe 发布时间: 2009-04-16
不错的想法。
作者: sueswriter 发布时间: 2009-04-16
楼上AD!封号!
作者: liexusong 发布时间: 2009-04-17
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28