I have a section in my code which uses file_get_contents to grab the url from the given web page. I also have a section in my code which scans the titles in each link value in my array. I want end up having an array similar to this :
Array(
Google => array(
[title] => Google
[link] => http://www.google.com
)
)
but no values are saved to my array, even though i can't detect any errors
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
$file = file_get_contents($URL);
// grabs the urls from URL
if( strlen( $file )>0 ) {
$links[] = preg_match_all( "/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/", $file, $links);
}
function Titles() {
global $links;
$str = implode('',array_map('file_get_contents',$links));
error_reporting(E_ERROR | E_PARSE);
$titles = Array();
if( strlen( $str )>0 ) {
$titles[] = preg_match_all( "/\<title\>(.*)\<\/title\>/", $str, $title );
return $title;
return $links;
}
}
$newArray = array();
$j = 0;
foreach( $links as $key => $val ){
$newArray[$key] = array( 'link' => $val, 'title' => $title[1][$j++]);
}
print_r($newArray);
The following code does not seem to return anything
$links[] = preg_match_all( "/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/", $file, $links);
Try the following
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
$file = file_get_contents($URL);
// grabs the urls from URL
if (strlen($file) > 0) {
$links[] = preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $file, $links);
}
var_dump($links);
Output
array
0 =>
array
0 => string 'http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd' (length=55)
1 => string 'http://www.w3.org/1999/xhtml' (length=28)
2 => string 'http://www.theqlick.com' (length=23)
3 => string 'http://www.theqlick.com' (length=23)
1 =>
array
0 => string 'd' (length=1)
1 => string 'l' (length=1)
2 => string 'm' (length=1)
3 => string 'm' (length=1)
2 => int 4