note 58505 deleted from function.preg-match by colder

From: Date: Mon, 18 Jun 2007 18:27:47 +0000
Subject: note 58505 deleted from function.preg-match by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127615@lists.php.net to get a copy of this message
Note Submitter: max99x [at] gmail [dot] com ---- Here's an improvement on the URL detecting function written by [rickyale at ig dot com dot br]. It detects SRC, HREF and URL links, in addition to URLs in CSS code, and Javascript imports. It also understands html entities(such as &amp;) inside URLs. <?php function get_links($url) { if( !($body = @file_get_contents($url)) ) return FALSE; //Pattern building across multiple lines to avoid page distortion. $pattern = "/((@import\s+[\"']([\w:?=@&\/#._;-]+)[\"'];)|"; $pattern .= "(:\s*url\s*\([\s\"'`]*([\w:?=@&\/#._;-]+)"; $pattern .= "([\s\"']*\))|<[^>]*\s+(src|href|url)\=[\s\"']*"; $pattern .= "([\w:?=@&\/#._;-]+)[\s\"'`]*[^>]*>))/i"; //End pattern building. preg_match_all ($pattern, $body, $matches); return (is_array($matches)) ? $matches:FALSE; } ?> $matches[3] will contain Javascript import links, $matches[5] will contain the CSS links, and $matches[8] will contain the regular URL/SRC/HREF HTML links. To get them all in one neat array, you might use something like this: <?php function x_array_merge($arr1,$arr2) { for($i=0;$i<count($arr1);$i++) { $arr[$i]=($arr1[$i] == '')?$arr2[$i]:$arr1[$i]; } return $arr; } $url = 'http://www.google.com'; $m = get_links($url); $links = x_array_merge($m[3],x_array_merge($m[5],$m[8])); ?>

« previous php.notes (#127615) next »