...

View Full Version : preg_replace href



Eben
07-02-2010, 12:29 PM
Hi,
i'm trying to replace href in a tags using preg_replace,
i only got this with preg_match.


preg_match_all("/<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]/i", $content, $match);

i don't familiar with regex, can anyone help me here?

Keleth
07-02-2010, 05:23 PM
Replace href with what? Or remove them?

Eben
07-02-2010, 05:56 PM
Replace href with what? Or remove them?

I want to add http://example.com/script.php?site=HREF

Keleth
07-02-2010, 06:41 PM
Is this for user input? Or your own code? Basically, do you anticipate the input being possibly invalid (lack of double quotes, arbitrary spaces, etc)?

Eben
07-04-2010, 08:03 PM
$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 ;)

Eben
07-04-2010, 10:59 PM
Ohh,
that's only works on double quotes, can someone help me out :eek:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum