Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match_all()不返回任何内容!_Php_Preg Match All - Fatal编程技术网

Php preg_match_all()不返回任何内容!

Php preg_match_all()不返回任何内容!,php,preg-match-all,Php,Preg Match All,我在这里有点困惑。我正在测试我在电脑(wamp)上编写的一些脚本,但由于某些原因,当使用preg_match_all()时,什么都不起作用! 我甚至注释掉了大部分其他代码,以查看是否有干扰,但没有,仍然是一样的。使用preg_match_all()时会显示错误,但不会显示错误 非常感谢任何帮助 <?php define( "DB_USERNAME", "root" ); define( "DB_PASSWORD", "" ); define( "DB_SERVER", "localhos

我在这里有点困惑。我正在测试我在电脑(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);
#   
#}
?>


操作员不是那样工作的。它是一个乘数(表示“0或更多”),而不是通配符
(句点)是单字符通配符。因此,
*
是一个多字符通配符

试试这个:

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

因为您似乎想要解析一些PHP代码,所以最好使用适当的解析器。您可以使用PHP获取该代码的数组,然后对其进行迭代。

来吧,伙计。你已经在船上呆了足够长的时间,知道如何正确地做这件事。请发布一个您正在匹配的字符串的示例(否则我们怎么知道为什么没有匹配?),在代码中,只包含匹配的行,可能还有前后5行。如果您没有得到它,它将返回“nothing”,甚至不返回Array()@Phil我想当表达式中出现错误时,这是正常的行为。您的错误报告是否打开了?呃,“#”式的注释:)看起来您的第一个preg_匹配(查找“?php”)失败了?好的,对了,正则表达式工作正常,一点问题也没有$foo=“你好”;preg#u match#u all(“#foo#Us“,$foo,$matches);只返回一个空的“Array()”,因为没有匹配项。同样,preg_match_all(“#”)*\?>#Us“,$foo,$matches);将返回“Array()”,没有匹配项。但是,preg_match_all(“#”)*\?>#Us“,$data,$matches);一无所获!我不能接受这个问题的答案,但是谢谢,我现在要再输一次。我不能这么说,这仍然让我在想为什么preg_match_都没有回报。
#<\?php(?.*)\?>#Us