Parse info from remote location

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Parse info from remote location

Post by Cronikeys »

Code: Select all

<?php
$file = fopen ("http://www.example.com/", "r");
if (!$file) &#123;
    echo "<p>Unable to open remote file.\n";
    exit;
&#125;
while (!feof ($file)) &#123;
    $line = fgets ($file, 1024);
    /* This only works if the title and its tags are on one line */
    if (eregi ("<title>(.*)</title>", $line, $out)) &#123;
        $title = $out&#1111;1];
        break;
    &#125;
&#125;
fclose($file);
?>
This code is from php.net, and it works great, however what if I need to get something within tags that are NOT on the same line :|
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Perhaps....

Code: Select all

$file = fopen ("http://phpdn.jam.nu/Tagfinding/test.html", "r"); // exists, check its source to see what it looks like...
    if (!$file) &#123;
        echo "<p>Unable to open remote file.\n";
        exit;
    &#125;
    $buffer = '';
    while (!feof ($file)) &#123;
        $buffer .= fgets ($file, 1024);
    &#125;
    fclose($file);
    if (eregi ("<sometag>(.*)</sometag>", $buffer, $out)) &#123;
        echo $title = $out&#1111;1]; // remove echo to just set the variable with the result...
    &#125;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's also file_get_contents() instead of the fopen()/fread()/fclose() routine, and using preg_match() or preg_match_all() instead of ereg*..

as another avenue..
Post Reply