简体   繁体   中英

preg replacing a string in PHP

I'm currently writing a code obfuscator for PHP in PHP, just to improve my skills with regex syntax.

The goal is to base64_encode strings before any obfuscating processes, and decode it after any obfuscating processes. On standard strings like "foo bar" or 'foo bar' everything works, but it does not work with string containing escaped (double/simple) quotes like this one : 'return \\'"\\'.base64_encode($matches[0]).\\'"\\';'

As you can see, I'm trying to obfuscate my own code, which seems to be fun. But my own code contains specific regex strings, which aren't correctly parsed.

Here is the code in charge of encoding a string starting and ending with simple quotes (the code in charge to do the same thing with double quote is almost the same) :

    $this->output = preg_replace_callback('/(?<!\\\\)\'(.*)(?<!\\\\)\'/isU', create_function(
            '$matches',
            'return "\'".base64_encode($matches[0])."\'";'
        ), $this->output
    );

Any ideas ?

please minimize your problem (post not working piece of code that is as simple as possible).

$test = "bla blah 'match \' this' blah blah";
$test = preg_match('/(?<!\\\\)\'(.*)(?<!\\\\)\'/isU', $test, $matches);
var_dump($matches);

works as charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM