Page 1 of 1

Parse info from remote location

Posted: Sun Jan 16, 2005 2:10 pm
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 :|

Posted: Sun Jan 16, 2005 3:33 pm
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;

Posted: Sun Jan 16, 2005 7:30 pm
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..