On 20-2-2014 23:59, Daevid Vincent wrote:
The trim() is still not working right. You can change it to MD5() and see that the value doesn't change.
While not needed in this particular case/regex, I'm curious why or more importantly how to process the matched part?
This simply has to do with the fact that you're trim()'ing the string
before it's processed as an argument to preg_replace.
Let's pick apart the string:
preg_replace(
"/<\?=(.+?);?\s*\?/",
'<?= ' . md5(
"$1"
) . ' ?',
'Username: <?= $foo; ?>'
) . "\n";
This could also be rewritten as:
$m = md5("$1");
preg_replace("/<\?=(.+?);?\s*\?/", "<?=$m ?", 'Username:
<?= $foo; ?>')."\n";
Or even clearer:
---
$pattern = "/<\?=(.+?);?\s*\?/";
$m = md5("$1"); // note that you're running md5 here
$replace = '<?=' . $m .' ?';
$subject = 'Username: <?= $foo; ?>';
$result = preg_replace($pattern, $replace, $subject); // and only
getting to the preg_replace here. AFTER the md5 has ran
$return = $result . "\n";
---
As you can see, all that's hapenning is that you're running md5("$1")
and inserting the literal output of that string ($1 does not get
replaced with anything since preg_replace hasn't ran yet). Which in turn
means that all you get is the exact same output, for all your possible
inputs.
What you're looking for (if you want to run a callback-function) is the
preg_replace_callback. Alternatively (and potentially VERY dangerous if
you don't have FULL control over your input) you could use the /e
modifier to eval the result, and have that "changed" code do whatever it
is you want.
Personally though, I'd go for the callback-route.
- Tul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php