0

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 ?

1
  • Why are you not obfuscating the whole string? Btw the i flag in your pattern is useless because there's nothing in the pattern that has a different representation in the "other" case. Commented Jan 24, 2013 at 0:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I minimized my problem as much as I could.I tried your code (thanks by the way for your answer), but it doesn't work with a double quote in the middle of the string. That's pretty much what I would like to do, encoding a string regardless of the type of the delimiter, and regardless of the content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.