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?
 
hieloConnect With a Mentor Commented:
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
hieloCommented:
preg_match('#NTP:\s+([+-]\S+)#', $data,$match);
print_r($match);
0
 
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
 
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 BaldwinConnect With a Mentor Fixer 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
 
hieloConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.