Click to See Complete Forum and Search --> : search an html page for links


AngryPenguin
09-01-2007, 05:45 AM
Hi all, I'm trying to search a page for links and put the links into an array. Is there a specific function for doing this, or do I have to search for '<a href="' then read the link in.

I'm thinking of something like this:

$file = fopen("links.html","r");
while (!feof($file))
{
// get line from file
// check if it's a link
// put link into array
}
fclose($file);

Thanks very much if you can help.
Andy

Yelgnidroc
09-01-2007, 03:32 PM
Here's a bit of code that I wrote to extract e-mail addresses from a file that you could adapt by changing the $email_pattern to a url pattern.


$email_pattern='/[[:alnum:]][A-Za-z0-9_.-]*@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';



// Get existing e-mail address from SUBSCRIBER_LIST.csv

$current_subscribers = file_get_contents("SUBSCRIBER_LIST.csv");
$current_subscribers=strtolower($current_subscribers);
preg_match_all($email_pattern,$current_subscribers,$matches);

for($i=0;$i<count($matches[0]);$i++)
echo $matches[0][$i].'<br />';