View Full Version : preg_replace issue
02-21-2010, 04:11 PM
I have a problem with the preg_replace function and using arrays with the backreferences.
So i have :
$code=preg_replace('#<!-- VAR ([a-zA-Z0-9\_\-\+\./]+) -->#',$this->output['$1'], $outfile);
where in output array's key is that of what is in the $1 reference, so this should work i think, but it returns nothing why is this? Ive tested the backreference and that comes out with what it should but it doesnt work when used with the array.
02-21-2010, 04:37 PM
Sorry, I have deleted the first post. I think I've better give some better explanation. The example you have posted implies that you would like to use the extracted pattern as index in an array. To be able to use this, you would need to make the whole replacement a string and use the e-modifier. Similar to how it is shown at Example #4 at the Manual page for preg_replace() (http://php.net/preg_replace). I think it could be something like this:
$code=preg_replace('#<!-- VAR ([a-zA-Z0-9\_\-\+\./]+) -->#e','$this->output[\'$1\']', $outfile);
My example has not been checked. Also since I do not know the task, it could possibly not do which is necessary. But it could give you an idea I think.
Edit: I have to say this too I think: Please also be very careful with the e-modifier. It is as dangerous as using eval() because it also makes a function execute some PHP code given as a string. Please use preg_replace() with the e-modifier only if you are absolutely sure no attacker would ever be able to substitute any code as the replacement string or part of the replacement string. e-modifier is very dangerous and should be used with great caution (if used at all).
02-21-2010, 04:39 PM
EDIT : Thanks! Works now, been trying to fix it for ages.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.