homepage Welcome to WebmasterWorld Guest from 23.23.12.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP preg_replace
modifying urlsís
humpo

10+ Year Member



 
Msg#: 7125 posted 12:38 am on Feb 27, 2005 (gmt 0)

Never been able to master regular expressions but some how mange, but not with this one.

Need the regular expression for modifying hrefs(so not other urls in images etc)

Example change
<a href=http://www.site1.com> to <a href="www.site2.com?q=http://www.site1.com">

Need it to work with urls that use relative paths so change
<a href="/"> to <a href="www.site2.com?q=/">

Be nice if it worked with all possible urls
<a href="http://www.site1.com">
< a href=Ďhttp://www.site1.comí>
<a href=http://site1.com>
Etc

Iíve found a few similar examples and near working bits of code but i rarely use preg_replace and donít know enough to get them to work.

Any help with this will be greatly appreciated thanks.

 

ogletree

WebmasterWorld Senior Member ogletree us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 7125 posted 1:00 am on Feb 27, 2005 (gmt 0)

Maybe you can use parse_url to help with that.

humpo

10+ Year Member



 
Msg#: 7125 posted 1:26 am on Feb 27, 2005 (gmt 0)

Thanks ogletree

Iím being a bit thick, so i better stick with the preg_replace for now.

Not sure how i would use parse_url to achieve what i want.

I want to parse an entire html file and replace all the hrefís with another url, using the old url/relative path as a parameter.

deizu

5+ Year Member



 
Msg#: 7125 posted 4:47 am on Feb 27, 2005 (gmt 0)

You can try the following. No effort is made to verify the original url string is a valid url.

$content = <<<EOQ

<a href="http://www.google.com"></a>
<a href=http://www.ala.com></a>

EOQ;

$content = preg_replace('#(<a[^>]+href="?)([^>" ]+)("?[^>]*>)#is','\\1http://www.site2.com?q=\\2\\3', $content);
print $content;

[edited by: eelixduppy at 12:37 pm (utc) on Mar. 20, 2008]
[edit reason] disabled smileys [/edit]

humpo

10+ Year Member



 
Msg#: 7125 posted 10:01 am on Feb 27, 2005 (gmt 0)

Thanks deizu that's perfect!
:)

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 7125 posted 11:59 pm on Feb 27, 2005 (gmt 0)

and welcome to WebmasterWorld, deizu.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved