The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
PHP - Variable in preg_replace pattern not working
Discuss Variable in preg_replace pattern not working in the Regex Programming forum on Dev Shed. Variable in preg_replace pattern not working Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 9th, 2012, 08:54 PM
|
 |
Contributing User
|
|
Join Date: Feb 2004
Posts: 173
  
Time spent in forums: 1 Day 9 m 24 sec
Reputation Power: 12
|
|
|
PHP - 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!
|

January 9th, 2012, 10:49 PM
|
 |
Lost in code
|
|
|
|
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";
|

January 9th, 2012, 11:37 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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
|

January 10th, 2012, 08:16 AM
|
 |
Contributing User
|
|
Join Date: Feb 2004
Posts: 173
  
Time spent in forums: 1 Day 9 m 24 sec
Reputation Power: 12
|
|
|
Thanks, I will give these ideas a try & keep you posted.
|

January 10th, 2012, 08:51 AM
|
 |
Contributing User
|
|
Join Date: Feb 2004
Posts: 173
  
Time spent in forums: 1 Day 9 m 24 sec
Reputation Power: 12
|
|
|
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!
|

January 10th, 2012, 06:56 PM
|
 |
Lost in code
|
|
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|