For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2004 > Re: extract links AND description from html









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: extract links AND description from html
Janwillem Borleffs

2004-10-23, 3:55 pm

Nils Jansen wrote:
> as a result from the script that i'm searching for, i want to get the
> full
>
> <a href=http://www.blabla.com/test/d.html>DESCRIPTOIN</a>
>
> can anybody give me some hint, how to do this?


Try this (remark: array_combine is a PHP 5 specific function, see the manual
entry for this function on php.net for a PHP 4 example);

<?php

// Fetch the content
$file = file_get_contents("http://www.php.net/");

// Construct the regular expression
// (does not accept image links)
$reg = "#<a.*href\s*=\s*(\"|')?([^\"'>]+).*>([^<>]+)</a>#i";

// Parse $file
if (preg_match_all($reg, $file, $matches)) {
print "<pre>";
print_r(array_combine($matches[2], $matches[3]));
print "</pre>";
}

?>


HTH;
JW



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com