我逐行读取php文件和存储在php数组中文件内容(一些代码行
我的php文件行数组
$old_line_arr = new array(
"define ( 'name', '' );"
"//define ( 'age', '' );"
" //define ( 'ID', '' );"
)我想检查给定的行数组是否有注释
isComment($old_line_arr[0]){
echo $old_line_arr[0].'commented';
}如何编写isComment函数?在php函数中有没有任何内建来检查给定的php是否已被注释。
发布于 2013-07-03 19:34:06
您可以像这样创建一个function
function isComment($str) {
$str = trim($str);
$first_two_chars = substr($str, 0, 2);
$last_two_chars = substr($str, -2);
return $first_two_chars == '//' || substr($str, 0, 1) == '#' || ($first_two_chars == '/*' && $last_two_chars == '*/');
}示例:echo isComment($old_line_arr[0]) ? 'comment' : 'not a comment';
发布于 2013-07-03 19:33:28
快速和肮脏,可能需要更多的错误处理代码来处理各种情况:
$string = "//define('ID', '');";
$tokens = token_get_all("<?php $string");
if ($tokens[1][0] == T_COMMENT) {
// it's a comment
} else {
// it's not
}https://stackoverflow.com/questions/17446739
复制相似问题