Page 1 of 1

php guru's help needed

Posted: Sun Dec 03, 2006 8:32 am
by iffo
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: Select all

$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: Select all

-Offer id 1773
and sometimes like

Code: Select all

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

I will appreciate your help.

Thanks

Posted: Sun Dec 03, 2006 8:37 am
by feyd
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
preg_match_all() after you've determined the line contains at least one number in the format you are looking for.

Posted: Sun Dec 03, 2006 8:43 am
by ok
Try somethink like this:

Code: Select all

<?php
//Assuming $contents contains only the "Offer id " line (read_file()...)
$what2find = "Offer id ";
$start_pos = strpos($contents, $what2find);
if($start_pos === false)
{
 echo ":(";
}
else
{
 $start_pos = $start_pos + strlen($what2find);
 //Read the rest of the line
 $rest = substr($text, $start_pos, strlen($text));
 //Analyze the 158 and 1295 and ... part
 if(is_numeric($rest))
 {
  echo ":)";
  echo $rest;
 }
 $rest_arr = explode(" ", $rest);
 echo ":)";
 for($i = 0; $i < count($rest_arr); $i=$i+2)
 {
  echo $rest_arr[$i];
 }
}
?>
Don't know if this code works, but you can try...

Posted: Sun Dec 03, 2006 4:11 pm
by duk
in my opinion you just need a " || " (or) in the if statement, with another preg_match trying to detect the other way....