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
|
Tricky PHP preg_replace
Discuss Tricky PHP preg_replace in the Regex Programming forum on Dev Shed. Tricky PHP preg_replace 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:
|
|
|

April 5th, 2009, 03:44 PM
|
 |
Contributing User
|
|
|
|
|
Tricky PHP preg_replace
I’m trying to edit a RSS feed so the syntax is compatible with a RSS parsing script my site uses. Here is the RSS feed.
Here are the two strings which I need to edit:
<media:content width="1600" url="http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s1600/DSC00242.JPG" medium="image" type="image/jpeg" height="1200"/>
<media:thumbnail width="288" url="http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s288/DSC00242.JPG" height="216"/>
I need to change them so they appear as:
<content>http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s1600/DSC00242.JPG</content>
<thumbnail>http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s288/DSC00242.JPG</thumbnail>
I’ve been trying for a long time now and have come up with a hideous mess of PHP!
Any ideas? Thanks.
Last edited by jleagle : April 5th, 2009 at 03:50 PM.
|

April 5th, 2009, 04:11 PM
|
|
|
How about this?
php Code:
Original
- php Code |
|
|
|
<?php $content = '<media:content width="1600" url="http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s1600/DSC00242.JPG" medium="image" type="image/jpeg" height="1200"/>'; $thumbnail = '<media:thumbnail width="288" url="http://lh3.ggpht.com/_YcQ1Dmq5PFY/SL-wC7SQVII/AAAAAAAAC6Q/NpfNgIZZDTA/s288/DSC00242.JPG" height="216"/>'; echo preg_replace('/^<media:(content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $content) . "\n"; echo preg_replace('/^<media:(content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $thumbnail); ?>
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

April 5th, 2009, 04:18 PM
|
 |
Contributing User
|
|
|
|
Thanks for the quick reply, i should have mentioned how i was fetching the feed. Here is my script:
php Code:
Original
- php Code |
|
|
|
<?php $rss = file_get_contents('http://pipes.yahoo.com/pipes/pipe.run?XML=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fjimeagle%2Falbumid%2F'.$ _GET ['a']. '%3Fthumbsize%3D288%26imgmax%3D1600&_id=_ps93kMh3hGd_2Ub3nBDOQ&_render=rss&itemname=entry'); $rss = preg_replace('/^<media:(content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $rss) . "\n"; $rss = preg_replace('/^<media:(content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $rss); ?>
I tried to adapt the code you gave me but it didnt work. I am hosting it here.
Last edited by jleagle : April 5th, 2009 at 04:22 PM.
|

April 5th, 2009, 07:00 PM
|
|
|
OK, how about this then?
php Code:
Original
- php Code |
|
|
|
$fp = file('http://jimeagle.com/site/pictures_rss.php?a=5241893999118690321'); foreach ($fp as $line) { echo preg_replace('/(\s+)<media:(content|thumbnail)\s.*url="([^\s"]+).*/', '$1<$2>$3</$2>', $line); }
|

April 5th, 2009, 07:21 PM
|
 |
Contributing User
|
|
|
|
|
Wow thanks alot, that works perfectly. I have been trying to do that for hours!!
|
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
|
|
|
|
|