Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php gur'us help needed 1

Status
Not open for further replies.

786snow

Programmer
Nov 12, 2006
75
Hi,

I have a text file notes.txt, which has following content

**********
Site information

-ustaxrelief1
-Offer id 1773

-New Creative
-Doing Targus validation in the controller.
-Lead is not going to the lead agent, instead going straight to the buyer.
-lead saved in the the posted_lead table.

*******************

I want to get the number 1773 out of it. I have a script that does it for me

Code:
$file = file("notes.txt"); 

foreach ( $file AS $line ) 
{ 
    if ( preg_match("/-Offer id (\d+)/", $line, $foo) ) 
    { 
        echo "The offer id is: " . $foo[1] . "\n"; 
    } 
}


Now my problem is sometime it is in following format
Code:
-Offer id 1773

and sometimes like

Code:
-Offer id 1773 and 1667

How can I change myscript so that it checks if it has two number and if it does, grab the second number as well?

I will appreciate your help.

Thanks
 
The trick, I think, is to split the list of IDs into an array using split() on the string ' and '.

Assuming notes.txt reads something like:

[tt]Site information

-ustaxrelief1
-Offer id 1773

-New Creative
-Doing Targus validation in the controller.
-Lead is not going to the lead agent, instead going straight to the buyer.
-lead saved in the the posted_lead table.


Site information

-ustaxrelief1
-Offer id 1773 and 1942

-New Creative
-Doing Targus validation in the controller.
-Lead is not going to the lead agent, instead going straight to the buyer.
-lead saved in the the posted_lead table.[/tt]

Then the script:

Code:
<?php

$file = file('notes.txt');

foreach ( $file AS $line )
{
    if (preg_match ('/^-Offer id /', $line) !== 0)
    {
        $line = str_ireplace ('-Offer id ', '', $line);

        $id_array = split (' and ', $line);

        print_r ($id_array);

    }
}
?>

will output:

[tt]Array
(
[0] => 1773

)
Array
(
[0] => 1773
[1] => 1942

)
[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top