Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 dfrojd

dfrojd
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 04 September 2006 - 11:26 AM

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 11:29 AM

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

#3 dfrojd

dfrojd
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 04 September 2006 - 11:42 AM

OK then,

The text file xxx.txt looks like:

http://www.yahoo.com
http://www.google.com
http://www.thisismysite.com
...
...
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;
    }


?>


#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 11:43 AM

use file() and foreach()

<?php

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

?>


#5 dfrojd

dfrojd
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 04 September 2006 - 11:47 AM

Hey, great, THANX!

I´ll get to work and try that.

Dennis

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 11:52 AM

Not sure but prehaps:
$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;
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users