-1

我在这里有点难过。我正在测试我在计算机上编写的一些脚本(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);
#   
#}
?>
4

2 回答 2

1

看起来您想解析一些 PHP 代码,最好使用适当的解析器。您可以使用 PHP获取该代码的语言标记token_get_all数组,然后对其进行迭代。

于 2010-07-10T09:03:50.470 回答
0

*操作员不是那样工作的。它是一个乘数(意思是“0 或更多”)而不是通配符。.(a period) 是单字符通配符。因此,.*是一个多字符通配符。

尝试这个:

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

或者,由于您似乎想要匹配 和之间的所有内容<?php?>这样做会更简单:

#<\?php(?.*)\?>#Us
于 2010-07-10T08:36:56.813 回答