#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    173
    Rep Power
    13

    Variable in preg_replace pattern not working


    Hi,
    I'm trying to use preg_replace to update links in my code. My links would take this form:
    Code:
    OLD LINK: <?php print(ABS_URL) ?>page/5/my-old-page
    NEW LINK: my-new-url
    There are 200+ different pages & I need to update all of the internal links within them. I'm trying to use preg_replace w/ a variable for the old url & a variable for the new url.

    The urls have forward slashes in them so I'm using | as a delimiter in the preg_replace function.

    If I print out the old & new variables, the values are correct. Here's the code that isn't working:
    Code:
    $text = preg_replace('|<\?php print\(ABS_URL\); \?>'.$old_url.'|', $new_url, $text);
    However, if I type out the old url, it does work:
    Code:
    $text = preg_replace('|<\?php print\(ABS_URL\); \?>page/5/my-old-page|', $new_url, $text);
    I've been on this for 7 hrs now. I could have done it by hand at this point but I need to use it on another site as well. Everthing I find says that that's the way to include a variable in the pattern but I can't get it to work. Any thoughts?
    Shazam!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Variables in preg_replace work exactly like variables in any other location, although you may want to run it through preg_quote first to escape any regex special characters.

    The fact that typing out the old URL works when using the variable does not work implies that the variable does not contain the value that you think it does. If I had to guess, I would say this is most likely caused by having some control character in the $old_url value that isn't visible when you print it out and confirm it visually.

    Try using strlen on $old_url and see whether it's length is the same as the number of characters that are visible when you print it out.

    If all else fails, you can easily see exactly what characters are in $old_url using this code:
    PHP Code:
    for($i=0;$i<strlen($old_url);$i++) echo "[{$old_url[$i]}] " ord($old_url[$i]) . " <br />\n"
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Beautiful advice by E-Oreo.

    And: I would also break it down into smaller components, using dummy strings, to see if you might have a problem with your syntax.
    This code seems to do what you want (try to run it).
    Input:
    Code:
    blah <?php print(ABS_URL) ?>page/5/my-old-page blah
    Code:
    PHP Code:
    $string='blah <?php print(ABS_URL?>page/5/my-old-page blah';
    $old='page/5/my-old-page';
    $new='my-new-url';
    $pattern=',<\?php print\(ABS_URL\)\s*\?>'.$old.',';
    $s=preg_replace($pattern,$new,$string);
    echo $s;
    Output:
    Code:
    blah my-new-url blah
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    173
    Rep Power
    13
    Thanks, I will give these ideas a try & keep you posted.
    Shazam!
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    173
    Rep Power
    13
    Wow, thanks so much. E-Oreo, you saved the day. I printed out all the characters in the string & found that <?php print(ABS_URL); ?> was already a part of $old_url. I was duplicating that in the preg_replace function.

    And I couldn't see that it was in there because when I printed the variable to the screen, it actually ran the print function instead of displaying the code. And since ABS_URL didn't have a value anymore, it printed nothing.

    Whew, I'm so glad that's solved! Thanks again!
    Shazam!
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    And I couldn't see that it was in there because when I printed the variable to the screen, it actually ran the print function instead of displaying the code. And since ABS_URL didn't have a value anymore, it printed nothing.
    Actually not likely; the print function would never execute code like that. More likely, you were viewing the output in a web browser which interpreted <?php print(ABS_URL); ?> as an HTML tag and thus did not display it literally. It would however have been visible if you had used the view source tool in your web browser.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo