RE: [PHP] Regex Help for URL's [ANSWER]
| From: | Robert Samuel White | Date: | Tue, 16 May 2006 22:49:31 +0000 |
| Subject: | RE: [PHP] Regex Help for URL's [ANSWER] | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-236306@lists.php.net to get a copy of this message | ||
In case any one is looking for a solution to a similar problem as me, here
is the answer. I used the code from my original post as my guiding light,
and with some experimentation, I figured it out.
To get any URL, regardless of where it is located, use this:
preg_match_all("#\'http://(.*)\'#U", $content,
$matches);
This match anything similar to:
'http://www.domain.com/dir/dir/file.txt?query=blah'
This is useful, if for example, you have a tag like this one:
<A HREF="javascript:void(0);" ONCLICK="javascript:window.open =
'http://www.domain.com/dir/dir/file.txt?query=blah';">
Now, for tags which are in quotes, rather than single quotes, just use:
preg_match_all("#\"http://(.*)\"#U", $content,
$matches);
This is really only the first step.
In order to be useful, you need a way to process these urls according to
your own specific needs:
preg_match_all("#\'http://(.*)\'#U", $content,
$matches);
$content = preg_replace("#\'http://(.*)\'#U",
'###URL###', $content);
This will modify the $content variable to change all urls to ###URL###
You can then go through them one at a time to process them:
for ($count = 0; $count < count($matches[1]); $count++)