Jump to content

Archived

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

dfrojd

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

Recommended Posts

Hi folks,

I have a neat function that does WHOPPHIE with a given url.
I also have a text file [urls.txt] that consists of urls, each in a single line, that I want to be WHOOPIED.

Hmmm - now how do I do that?


Any help would be very much appreciated.
Dennis

Share this post


Link to post
Share on other sites
Never head of WHOOPHIE before. What is it? Also provide more information too about what you are trying along with a visual referencce too. PLus could you provide an example what is in urls.txt

Share this post


Link to post
Share on other sites
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;
    }


?>

Share this post


Link to post
Share on other sites
use file() and foreach()

[code]<?php

foreach (file('urls.txt') as $line) {
   WHOPPHIE($line); //whatever 'WHOPPHIE' does..
}

?>[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.