Last active
August 29, 2015 14:07
-
-
Save alash3al/a52001e89daa0ed28722 to your computer and use it in GitHub Desktop.
Extract links from source page or subject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A function to extract links from source | |
* @version 1.0 | |
* @author Mohammed Al Ashaal | |
* @copyright 2014 | |
* @license New BSD License | |
*/ | |
if ( ! function_exists('extract_links') ): | |
/** Links must have schema */ | |
define('LINKS_MUST_SCHEMA', 2); | |
/** links must hace path */ | |
define('LINKS_MUST_PATH', 4); | |
/** | |
* Extract links from a source page | |
* @param string $source | |
* @param integer $flags | |
* @return array | |
*/ | |
function extract_links($source, $flags = 0) | |
{ | |
$pattern = "((www\.|)([\p{N}\p{L}]+)\.[\p{N}\p{L}\.]+)"; | |
if ( LINKS_MUST_SCHEMA & $flags ) | |
$pattern = "([a-z]+:\/\/)" . $pattern; | |
else | |
$pattern = "([a-z]+:\/\/|)" . $pattern; | |
if ( LINKS_MUST_PATH & $flags ) | |
$pattern = $pattern . "((\/)(\S*))"; | |
else | |
$pattern = $pattern . "((\/|)(\S*))"; | |
if ( preg_match_all("/({$pattern})/ui", $source, $x) ) | |
return $x[1]; | |
else | |
return array(); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment