Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > extracting headlines from remote site with php

extracting headlines from remote site with php
Thread Tools
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Jan 8, 2004, 04:39 AM
 
i'm working on a site right now that contains a 'headlines' section that needs to grab headlines from a couple other sites and display the links. rss isn't an option, so i would like to use a php script to read the remote sites and grab the headline info.

from the php manual, i was able to come up with:
[php]
<?

$file = fopen ("http://www.example.com/", "r");
while (!feof ($file))
{
$line = fgets ($file, 1024);
if (eregi ("<a href=\"articles/>(.*)</a>", $line, $out))
{
$link = $out[0];
break;
}
}

fclose($file);

?>
[/php]

this basically does what i need it to, except that the eregi will only find matches if the opening and closing tags are on the same line. one of the sites i'm trying to grab a link from uses multiple lines:

[php]
<a href="articles/headline.htm">
Headline
</a>
[/php]

anyone have any ideas about how i can get this to work???
     
mzllr  (op)
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Jan 8, 2004, 05:14 AM
 
got it...

[php]
<?

$file = fopen("http://www.example.com/" ,"r");

$j = 0;

while (!feof($file))
{
$line = fgets($file, 1024);

if ("$j" == 1) { echo "$line</a>"; $j = 0; }

if (eregi("<a href=\"/articles/(.*)>", $line, $out))
{
echo "$out[0]"; $j = 1;
}
}

fclose ($file);

?>
[/php]

if a match is found with the eregi, the next pass through the loop will echo the next line and close the link...

if anyone has an idea on how to do this better, i'd appreciate any suggestions.
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Jan 8, 2004, 09:35 AM
 
look into the perl compatible regeps, and then learn perl-style regular expressions. They are very powerful, and can do things like this trivially.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:49 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2