PHP replace href

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
eben
Forum Newbie
Posts: 2
Joined: Fri Jul 02, 2010 4:43 pm

PHP replace href

Post by eben »

Hi,
I need to find and replace regex code for php.
I need to replace all HREF in A tags,
eg. href="index.html" to href="http://mysite.com/site=http://the_user_ ... index.html"

I can get the href using preg_match_all:

Code: Select all

    preg_match_all("/<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]/i", $content, $match);
I need to do it with preg_replace.

any help will be appreciated.
eben
Forum Newbie
Posts: 2
Joined: Fri Jul 02, 2010 4:43 pm

Re: PHP replace href

Post by eben »

Code: Select all

$content = preg_replace('#(<a\s[^>]*href)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#', '$1="http://example.com/site='.$_GET(['site'].'/$2$3' ,$content);
the solution in case someone needs it ;)
Post Reply