SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
Nov 19, 2006, 22:32 #1
- Join Date
- Oct 2006
- Posts
- 65
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In need of help with preg_replace
Hello all. I'm attempting to make an HTML fetcher type of script. So far I'm doing good, but I have stumbled across a problem: I'm trying to replace every link in the document with a link going through my site (for statistics). Something like this:
<a href="http://www.yahoo.com" title="...">
with
<a href="http://www.site.com/fetch?http://www.yahoo.com" title="...">
Sadly, I'm no good with Regex and don't expect to even remotely grasp the syntax anytime soon. So anyone mind showing me the proper regex to use for this kind of script?
Oh and I already know that a number of sites don't use the full URL, but rather something like /dogs/index.php to navigate through the site, but I think I can handle that on my own.
Thanks in advance.
-
Nov 20, 2006, 04:30 #2
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:$re = "~<a([^<>]+?)href=([\'\"])~si";
Code:$repl = "<a$1href=$2http://www.site.com/fetch?";
Code:$new = preg_replace($re, $repl, $html);
-
Nov 20, 2006, 11:12 #3
- Join Date
- Jun 2006
- Location
- Sweden
- Posts
- 73
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here´s another way:
PHP Code:preg_replace('/(?<=<a href=")(.*?")/', 'http://www.site.com/fetch?\\1', $string);
-
Nov 20, 2006, 14:15 #4
- Join Date
- Oct 2006
- Posts
- 65
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you tons, guys (or gals). I'll be sure to acknowledge you two (and this forum in general) with proper credit!
Bookmarks