note 53619 deleted from function.preg-match-all by colder
| From: | colder@php.net | Date: | Mon, 16 Apr 2007 16:59:49 +0000 |
| Subject: | note 53619 deleted from function.preg-match-all by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125464@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at m-bread dot com
----
Looking at the function from rickyale at ig dot com dot br below getting URLs from an html file, I
think this is slightly better:
function get_urls($string, $strict=true) {
$types = array("href", "src", "url");
while(list(,$type) = each($types)) {
$innerT = $strict?'[a-z0-9:?=&@/._-]+?':'.+?';
preg_match_all ("|$type\=([\"'`])(".$innerT.")\\1|i", $string,
&$matches);
$ret[$type] = $matches[2];
}
return $ret;
};
This only gets urls in quotes "...",
... or '...', but not mixed
quotes like `..." (thanks to w w w's note on the 'pattern syntax' page). If you
set the second parameter to false, then the function will give you any contents of attribute (so the
function can be used to get other attributes, such as alt). To make it more strict, the
'[a-z0-9:?=&@/._-]+?' can be replaced with a regular expression for a url.