Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 9th, 2012, 08:54 PM
shazam-fu's Avatar
shazam-fu shazam-fu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 173 shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level) 
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!

Reply With Quote
  #2  
Old January 9th, 2012, 10:49 PM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,936 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 8 h 35 m 14 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP

Quote:
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

Reply With Quote
  #3  
Old January 9th, 2012, 11:37 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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

Reply With Quote
  #4  
Old January 10th, 2012, 08:16 AM
shazam-fu's Avatar
shazam-fu shazam-fu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 173 shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 9 m 24 sec
Reputation Power: 12
Thanks, I will give these ideas a try & keep you posted.

Reply With Quote
  #5  
Old January 10th, 2012, 08:51 AM
shazam-fu's Avatar
shazam-fu shazam-fu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 173 shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level)shazam-fu User rank is Corporal (100 - 500 Reputation Level) 
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!

Reply With Quote
  #6  
Old January 10th, 2012, 06:56 PM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,936 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 8 h 35 m 14 sec
Reputation Power: 7053
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > PHP - Variable in preg_replace pattern not working

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap