php guru's help needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

php guru's help needed

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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...
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

in my opinion you just need a " || " (or) in the if statement, with another preg_match trying to detect the other way....
Post Reply