preg_match_all()没有返回任何东西!

时间:2010-07-10 08:24:45

标签: php preg-match-all

我在这里有点难过。我正在测试我在计算机上编写的一些脚本(wamp),但由于某种原因,使用preg_match_all()时无效! 我甚至会注释掉大部分其他代码,看看是否存在干扰,但是没有,仍然是相同的。使用preg_match_all();

时会显示错误但不会显示错误

任何非常感谢的帮助;

<?php

define( "DB_USERNAME", "root" );
define( "DB_PASSWORD", "" );
define( "DB_SERVER", "localhost" );
define( "DB_NAME", "s_framework" );

$CON = mysql_connect( DB_SERVER, DB_USERNAME, DB_PASSWORD ) or die ( mysql_error() );
$DB = mysql_select_db( DB_NAME, $CON );

$query = "SELECT * FROM `files` ORDER BY ID";
$query = mysql_query( $query, $CON ) or die ( mysql_error() );

$remove_comments = true;
$remove_white_space = false;
$new_folder = 'new_encrypt/';
$encryption_code = 'foobar';
$path_array = array();
$user_defined_functions = array('name' => array(), 'encode' => array());
$user_defined_variables = array('name' => array(), 'encode' => array());
$user_defined_constants = array('name' => array(), 'encode' => array());

if( ! file_exists( $new_folder ) ) {
    mkdir( $new_folder, 0700);
}

while( $rows = mysql_fetch_array( $query ) ){

    $name = $rows['NAME'];
    $location = $rows['LOCATION'];
    $path = $location . $name;
    $path_array['path'][] = $path;
    $path_array['name'][] = $name;
    $path_array['location'][] = $location;

    $lines = file($path);
    $data = implode("", $lines);

    preg_match_all("#<\?php*((?!\?>).)*\?>#Us", $data, $matches);

    print_r($matches);

    #foreach ($lines as $line_num => $line) {

    #   if( preg_match( "#function\s+([^\s\(]+)\s?\([^\)]+\)#is", $line, $match ) ){
    #       if( ! in_array( $match[1], $user_defined_functions['name'] ) ) {
    #           $user_defined_functions['name'][] = $match[1];
    #           $user_defined_functions['encode'][] = "v" . md5($match[1].$encryption_code);
    #       }
    #   }
    #   if( preg_match( '#\$([a-zA-Z_][a-zA-Z0-9_]*)#is', $line, $match ) ){
    #       if( ! in_array( $match[1], $user_defined_variables['name'] ) ) {
    #           $user_defined_variables['name'][] = $match[1];
    #           $user_defined_variables['encode'][] = "v" . md5($match[1].$encryption_code);
    #       }
    #   }
    #   if( preg_match( '#define\s?\(\s?[\'\"]([^\s\"\']+)#is', $line, $match ) ){
    #       if( ! in_array( $match[1], $user_defined_constants['name'] ) ) {
    #           $user_defined_constants['name'][] = $match[1];
    #           $user_defined_constants['encode'][] = "v" . md5($match[1].$encryption_code);
    #       }
    #   }
    #}

}

#foreach( $path_array['location'] as $key => $folder ) {

#   if( ! file_exists( $new_folder . $folder ) ) {
#       mkdir( $new_folder . ltrim($folder, "./"), 0700);
#   }
#   $lines = file($path_array['path'][$key]);
#   $data = implode("", $lines);
#   foreach( $user_defined_functions['name'] as $key2 => $f_name ) {
#       $data = str_replace( $f_name, $user_defined_functions['encode'][$key2], $data );    
#   }
#   foreach( $user_defined_variables['name'] as $key2 => $f_name ) {
#       $data = preg_replace( '#\$' . $f_name . "(\;|\s|\,|\[|-|\))#", '$' . $user_defined_variables['encode'][$key2] . "$1", $data );  
#   }
#   foreach( $user_defined_constants['name'] as $key2 => $f_name ) {
#       $data = preg_replace( "#([\"\']\s?\.\s?)" . $f_name . "#", "$1" . $user_defined_constants['encode'][$key2], $data );    
#   }
#   
#   $fp = fopen( $new_folder . ltrim($folder, "./") . $path_array['name'][$key], 'w');
#   fwrite($fp, $data);
#   fclose($fp);
#   
#}
?>

2 个答案:

答案 0 :(得分:1)

由于您似乎想要解析一些PHP代码,因此最好使用正确的解析器。您可以使用PHP的token_get_all获取该代码的language tokens数组,然后迭代它。

答案 1 :(得分:0)

*运算符不能那样工作。它是乘数(意思是“0或更多”)而不是通配符。 .(句点)是单字符通配符。因此,.*是一个多字符通配符。

试试这个:

#      v           v
#<\?php.*((?!\?>).).*\?>#Us

或者,既然您希望匹配<?php?>之间的所有内容,那么执行此操作会更简单:

#<\?php(?.*)\?>#Us