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

    Join Date
    May 2004
    Posts
    228
    Rep Power
    26

    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.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    How about this?
    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<img src="http://images.devshed.com/fds/smilies/frown.gif" border="0" alt="" title="Frown" class="inlineimg" />content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $content) . "\n";
    echo preg_replace('/^<media<img src="http://images.devshed.com/fds/smilies/frown.gif" border="0" alt="" title="Frown" class="inlineimg" />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!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    228
    Rep Power
    26
    Thanks for the quick reply, i should have mentioned how i was fetching the feed. Here is my script:
    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<img src="http://images.devshed.com/fds/smilies/frown.gif" border="0" alt="" title="Frown" class="inlineimg" />content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $rss) . "\n";
    $rss = preg_replace('/^<media<img src="http://images.devshed.com/fds/smilies/frown.gif" border="0" alt="" title="Frown" class="inlineimg" />content|thumbnail)\s.*url="([^\s"]+).*/', '<$1>$2</$1>', $rss);
    echo $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.
  6. #4
  7. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    OK, how about this then?
    php Code:
    $fp = file('http://jimeagle.com/site/pictures_rss.php?a=5241893999118690321');
    foreach ($fp as $line)
    {
        echo preg_replace('/(\s+)<media<img src="http://images.devshed.com/fds/smilies/frown.gif" border="0" alt="" title="Frown" class="inlineimg" />content|thumbnail)\s.*url="([^\s"]+).*/', '$1<$2>$3</$2>', $line);
    }

    Comments on this post

    • jleagle agrees
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    228
    Rep Power
    26
    Wow thanks alot, that works perfectly. I have been trying to do that for hours!!

IMN logo majestic logo threadwatch logo seochat tools logo