php stripping a particular result from an output

I am using php to grab a shell output that lists all the meetme channels for me. I just want to grab the Conf Num that has 0001 from the bottom up. In my example below, I would like to assign variable: $confnum="32";

Here is my code so far:

$output = shell_exec("asterisk -rx 'meetme list'");

echo $output;

Please help me take the results from $output.

Here's what you'll get when executing the meet.php file

[root@C1033-TF agi-bin]# php meet.php
Conf Num       Parties        Marked     Activity  Creation  Locked
67             0001       N/A        00:00:16  Dynamic   No    
28             0001       N/A        00:00:19  Dynamic   No    
65             0001       N/A        00:01:14  Dynamic   No    
42             0001       N/A        00:01:18  Dynamic   No    
32             0001       N/A        00:04:18  Dynamic   No    
* Total number of MeetMe users: 5

Please keep in mind that sometime there will be more than just 0001 Parties in the Conf Num. In the example above, I just want to grab the line:

32             0001       N/A        00:04:18  Dynamic   No

This is the last line that has 0001 therefore assign $confnum="32"; to it.

Any kind of help I can get on this is greatly appreciated.

Answers


You can use exec instead of shell_exec and iterate through the output lines:

<?php
$output = array();
exec("asterisk -rx 'meetme list'", $output);

foreach ($output as $line) {
   if (preg_match('/^32\s/', $line)) { //Check line starts with 32
      echo $line;
   }
}

Edit:

<?php
$output = array();
exec("asterisk -rx 'meetme list'", $output);

$lines = sizeof($output);    
for ($i = $lines -1; $i >=0 ; $i--) {
   if (preg_match('/^(\d+)\s+0001\s/', $output[$i], $matches)) { //Check line contains 0001
      $firstNumber = $matches[1];
      echo $firstNumber;
      break;
   }
}

Okay, assuming Linux, you can do this purely in shell:

  • filter last line: grep -v 'Total number of MeetMe isers'
  • filter first line: grep -v 'Conf Num'
  • and print only one Conf Num: awk 'BEGIN{ result=""; } {if( $2 == "0001"){result=$1;}} END {print result;}'

So the whole code:

$output = shell_exec("asterisk -rx 'meetme list' | grep -v 'Total number of MeetMe isers' | grep -v 'Conf Num' | awk 'BEGIN{ result=\"\"; } {if( \$2 == \"0001\"){result=$1;}} END {print result;}'");
// $output should contain your data :)

Or use preg_match_all():

$output = shell_exec("asterisk -rx 'meetme list'");
$matches = array();
$result = null;
preg_match_all('~^\s*(\\d+)\\s+0001~', $output, $matches, PREG_SET_ORDER);
foreach( $matches as $match ){
  $result = $match[1];
}

In reaction to comment:

You should study regular expression syntax and meaning of \d and +. \d+ will match 0, 01 or 00000000000000000000000000000000 :).


Need Your Help

Orchard CMS Data Import

asp.net-mvc-3 data import orchardcms

I am attempting to import data into Orchard CMS. I have a custom type, made up of parts and fields, and I am using the ImportExport module. I have exported some data from Orchard to ensure the XML

How to use visual controls in Indy? (Delphi)

delphi indy

My server must print some reports about its work. How can I use visual objects such as labels, edit boxes in the OneEecute event?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.