Jump to content

how 2 use file xxx.txt, row by row as input for php function


dfrojd

Recommended Posts

OK then,

The text file xxx.txt looks like:

[nobbc]http://www.yahoo.com
http://www.google.com
http://www.thisismysite.com
...
...[/nobbc]
and so on


the code I have up to now accepts any one of these urls in a dialog box on a page called index.php in a form and gives this url to a php script like the one below.

The script stores whatever it has done in a database.

What I want is that the php script below accepts the text file directly as input and works on it line for line.

CODE:
<?

$content = $_POST; // ************* Here come the url from a dialog box one page before...
$content = file_get_contents($content['url']);

// ******************************** Now a lot of stuff (WHOOPHIE) is done to that file...

// BEZEICHNUNG
preg_match("=<h1[^>]*>(.*)</h1>=siU", $content, $hit);
$data['obj_bez'] = trim($hit[1]);

// KURZTEXT
preg_match("=<h2[^>]*>(.*)</h2>=siU", $content, $hit);
$data['obj_krz'] = trim($hit[1]);

...
...
...

// ******************************* and store array (WHOOPHIE) in a database...


$insert_data = array(
  'obj_bez'=> $data['obj_bez'],
  'obj_krz'=> $data['obj_krz']
);
DB_Sql::query(DB_Sql::insert('objekte', $insert_data));
$insert_id = mysql_insert_id();
if(DB_Sql::error()) {
    echo DB_Sql::error();
    exit;
    }


?>
Not sure but prehaps:
[code]$contents = $_POST; // ************* Here come the url from a dialog box one page before...
$contents = file_get_contents($contents['url']);

$contents = str_replace(array("\r\n", "\r", "\n"), "\n", $contents);

$contents = explode("\n", $contents);

foreach($contents as $url)
{
    // ******************************** Now a lot of stuff (WHOOPHIE) is done to that file...

    // BEZEICHNUNG
    preg_match("=<h1[^>]*>(.*)</h1>=siU", $url, $hit);
    $data['obj_bez'] = trim($hit[1]);

    // KURZTEXT
    preg_match("=<h2[^>]*>(.*)</h2>=siU", $url, $hit);
    $data['obj_krz'] = trim($hit[1]);


    // ******************************* and store array (WHOOPHIE) in a database...


    $insert_data = array( 'obj_bez'=> $data['obj_bez'],
                          'obj_krz'=> $data['obj_krz']
                        );

    DB_Sql::query(DB_Sql::insert('objekte', $insert_data));

    $insert_id = mysql_insert_id();

    if(DB_Sql::error())
    {
        echo DB_Sql::error();
        exit;
    }
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.