views:

40

answers:

2

Hi, im a little stumped here. Im testing some scripts that I have wrote on my computer (wamp) but for some reason when using preg_match_all() nothing works! I even comment out most of the other code to see if something was interfering but no, still the same. Errors do show but not when using preg_match_all();

any help much appreciated;

<?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);
#   
#}
?>
A: 

The * operator doesn't work like that. It is a multiplier (meaning "0 or more") and not a wildcard. . (a period) is the single-character wildcard. Therefore, .* is a multiple-character wildcard.

Try this:

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

Or, since it seems you want to match everything between <?php and ?> then, it would be much simpler to do this:

#<\?php(?.*)\?>#Us
amphetamachine
+1  A: 

As it seems that you want to parse some PHP code, better use a proper parser. You can use PHP’s token_get_all to get an array of language tokens of that code then iterate it.

Gumbo
See also this related question: http://stackoverflow.com/questions/3166714
Gumbo
I cant accept as the answer to the problem, but thx, I'll go another rout now, thankyou.
Phil Jackson
@Phil Jackson: Maybe you’re just asking the wrong question.
Gumbo
I can't say that, it still is making me think why preg_match_all returns nothing.
Phil Jackson