Page 1 of 1

PHP replace href

Posted: Fri Jul 02, 2010 4:51 pm
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.

Re: PHP replace href

Posted: Sun Jul 04, 2010 1:01 pm
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 ;)