note 58505 added to function.preg-match

From: Date: Sun, 06 Nov 2005 17:11:21 +0000
Subject: note 58505 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-98165@lists.php.net to get a copy of this message
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])); ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58505 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58505&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58505&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98165) next »