4

Right, i am trying to grab links from a web page. the links are storred in the array $links.

Is there a way for me to grab the title (from the function below) from each link in the array. would that be a multidimensional array? how can i do this?

$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab  
// grabs the urls from URL 
$file  = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
   $links[] = url_to_absolute($URL, $theelement->href);
} 
print_r($links);


    function getTitle($links) 
    {
      //change it for the original titles. 
      $str = file_get_contents("http://www.theqlick.com"); 
      if ( strlen( $str )>0 ) {
        preg_match( "/\<title\>(.*)\<\/title\>/", $str, $title );
     return $title[1];
    }
 } 

 $metatitle = getTitle();
 echo $metatitle;
FirmView
  • 3,130
  • 8
  • 34
  • 50
Noah Smith
  • 203
  • 4
  • 9
  • parsing HTML with regex is a bad idea. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Jessedc Sep 16 '12 at 11:56

1 Answers1

1

I don't have the right libraries installed to test this, but it should give you an idea of where to start:

$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab  
// grabs the urls from URL 
$file  = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
    $link = array();
    $link['url'] = url_to_absolute($URL, $theelement->href);
    $link['title'] = getTitle($link['url']);
    $links[] = $link;
} 
print_r($links);

function getTitle($url) 
{
    $file = file_get_html($url);
    $titles = $file->find('title');
    if (is_array($titles)) {
        return $titles[0];
    } else {
        return null;
    }
} 
Luke Mills
  • 1,616
  • 1
  • 11
  • 18