Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts

    preg_replace issue

    I have a problem with the preg_replace function and using arrays with the backreferences.

    So i have :
    PHP Code:
    $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.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    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(). I think it could be something like this:
    PHP Code:
    $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).
    Last edited by SKDevelopment; 02-21-2010 at 04:45 PM.

  • Users who have thanked SKDevelopment for this post:

    Killermud (02-21-2010)

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    EDIT : Thanks! Works now, been trying to fix it for ages.
    Last edited by Killermud; 02-21-2010 at 04:41 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •