php script to find a matching word and parse the number after it in a variable

I have a script that breaks down each line by itself, what I need to do is to have the php script to look at each line and if the first word = NTP: then to get the number after it and assign it echo that number only.

so say you have these three lines
$data= "ORDER 5434 processed on time
 NTP: +44332.43 was accepted
 UTC: +5 approved";

so if I pass these three lines in, it should catch that NTP: and print +44332.43

what is the best way to do it. It is simple but there are so many string functions I do not know which to use and I have been a bit rusty.
LVL 5
onlinerackAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
preg_match('#NTP:\s+(.+)#', $data,$match);
print_r($match);
0
onlinerackAuthor Commented:
the + can also be a -  and I would like that to be captured and be displayed.
0
hieloCommented:
preg_match('#NTP:\s+([+-]\S+)#', $data,$match);
print_r($match);
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Dave BaldwinFixer of ProblemsCommented:
Here's a demo of one way to do it.  Displays the whole string first and then extracts the value after NTP:.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Order Process</title>
</head>
<body>
<?php 
$data= "ORDER 5434 processed on time
 NTP: +44332.43 was accepted
 UTC: +5 approved";

 echo $data."<br><br>";
 $pieces = explode(" ", $data);
 reset($pieces);
 $ii = 9; // something greater than 0
while (list(, $value) = each($pieces)) {
    $ii++;
		if(strcmp("NTP:",$value) == 0) $ii = 0;
		if($ii == 1) { 
			echo $value;
			break;
			}
	}

 ?>
</body>
</html>

Open in new window

0
hieloCommented:
did you try my last post?
$matches[1]

should have ONLY the part aft NTP.

$data= "ORDER 5434 processed on time
 NTP: +44332.43 was accepted
 UTC: +5 approved";

preg_match('#NTP:\s+(.+)#', $data,$match);

echo $matches[1];//should give you  +44332.43
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onlinerackAuthor Commented:
I will give that a try and let you know tomorrow... thank you very much guys.

if I can add one twist would be nice but this is optional if too much work do not worry about it.

say I have 6 lines, with multiple NTP: how would you word to catch the numbers:

$data="ORDER 5434 processed on time
 NTP: +44332.43 was accepted
 UTC: +5 approved
ORDER 5434 processed on time
 NTP: -0.3223.43 was accepted
 UTC: +6 approved";
0
Dave BaldwinFixer of ProblemsCommented:
This is one way.  Just remove the break and let it keep on finding the NTP:'s.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Order Process</title>
</head>
<body>
<?php 
$data= "ORDER 5434 processed on time
 NTP: +44332.43 was accepted
 UTC: +5 approved
ORDER 5434 processed on time
 NTP: -0.3223.43 was accepted
 UTC: +6 approved";

 echo $data."<br><br>";
 $pieces = explode(" ", $data);
 reset($pieces);
 $ii = 9; // something greater than 0
while (list(, $value) = each($pieces)) {
    $ii++;
		if(strcmp("NTP:",$value) == 0) $ii = 0;
		if($ii == 1) { 
			echo $value."<br>";
			}
	}

 ?>
</body>
</html>

Open in new window

0
hieloCommented:
based on my previous post, simply use preg_match_all() instead of preg_match():


preg_match_all('#NTP:\s+([+-]\S+)#', $data,$match);

// $match[1] is an array that contains all the NTP numbers
//print_r( $match[1] );
foreach($match[1] as $v)
{
  echo $v;
}
0
onlinerackAuthor Commented:
Thank you, I was able to take your recommendations and modify my script based on your recommendations which lead me to the solution.... very much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.