Problem using preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Problem using preg_replace

Post by winsonlee »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$body1[1] = "<title>afasfasf</title><link>http://www.kafa.com</link><description>afasfasf</description><category>afafasdf</category><pubDate>Sun, 30 Apr 2006 14:08:53 GMT</pubDate></item>";

echo preg_replace('/<title>(.*?)<\/title><link>(.*?)<\/link><description>(.*?)<\/description><category>(.*?)<\/category><pubDate>(.*?)<\/pubDate><\/item>/i', '$5', $body1[1]);

$body1[1] = "
                   <title>aalkdfkasflkasklfasfdlsafasfasf</title>
                   <link>http://www.kafa.com</link>
                   <description>skafdasfasf.</description>
                   <category>kweasfasff</category>
                   <pubDate>Sun, 30 Apr 2006 14:08:53 GMT</pubDate>
               </item>";

echo preg_replace('/<title>(.*?)<\/title><link>(.*?)<\/link><description>(.*?)<\/description><category>(.*?)<\/category><pubDate>(.*?)<\/pubDate><\/item>/i', '$5', $body1[1]);
My first preg_replace works, but somehow the second one doest work.What things did i miss out for the second preg_replace to handle the second string ??


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add \s* between the tags and add the s modifier after the i modifier.
Post Reply