note 53619 added to function.preg-match-all

From: Date: Tue, 07 Jun 2005 13:45:53 +0000
Subject: note 53619 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-90062@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53619 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53619&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53619&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53619&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53619&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53619&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53619&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90062) next »