I have grabbed some urls from Google search results using a regular expression. It has provided me the links in the format given below. Now, I just want the scheme and the host.
Regex to match the above mentioned URL's which are preceded by /url?q=
,
\/url\?q=\K.*?(?=\/&)
DEMO
OR
www\.[^.]*\.(?:org|com)
DEMO
Your PHP code would be,
<?php
$url = <<< 'EOT'
/url?q=http://www.fertile-focus.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CCsQFjAEOGQ&usg=AFQjCNEwG9ntbG0ZtqbqjJNSfVTlqQJYmg
/url?q=http://www.genetests.org/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CDgQFjAGOGQ&usg=AFQjCNFiux9o5YIUGP4P8B_oG_J6iD1Y6g
EOT;
$regex = '~\/url\?q=\K.*?(?=\/&)~';
preg_match_all($regex, $url, $matches);
var_dump($matches);
?>
Output:
array(1) {
[0]=>
array(2) {
[0]=>
string(28) "http://www.fertile-focus.com"
[1]=>
string(24) "http://www.genetests.org"
}
}
I'd use parse_url and parse_str to achieve this rather than regular expressions.
<?php
$url = "/url?q=http://www.fertile-focus.com/&sa=U&ei=dTTTU7L2A4egugSY44LgAQ&ved=0CCsQFjAEOGQ&usg=AFQjCNEwG9ntbG0ZtqbqjJNSfVTlqQJYmg";
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query,$result);
echo $result['q'];
DEMO