Jump to content

parsing an output


cr-ispinternet

Recommended Posts

Hi All,

 

Im using telnet in conjunction with mysql and php to run a media server

im working on...

 

Theres an output i would like ti parse if possible, but have not got a clue whre to start

heres the output

 

Password: ÿûÿü Welcome, Master > show piggy type : broadcast enabled : yes loop : no inputs /root/MediaPlayer/Audio/cryforyou.mp3 /root/MediaPlayer/Audio/FCMK.mp3 output : options instances instance name : default state : playing position : 0.351690 time : 84897960 length : 235052421 rate : 1000 title : 0 chapter : 0 seekable : 1 playlistindex : 1 > 

 

in essence id like to be able to parse it in the following way

 

ignore all this ( Password: ÿûÿü Welcome, Master > show piggy type : )

thn show line by line the follwing
broadcast enabled : yes
loop : no
inputs /root/MediaPlayer/Audio/cryforyou.mp3 
         /root/MediaPlayer/Audio/FCMK.mp3
output :
options instances instance name :
default state :
playing position : 0.351690
time : 84897960
length : 235052421
rate : 1000
title : 0
chapter : 0 
seekable : 1 
playlistindex : 1

 

Is there an easy way to parse this file using php

 

Alan

Link to comment
Share on other sites

It's a bit lacking in suitable delimiters so let's add some with strtr() then split up the text;

<?php
$str = 'Password: ÿûÿü Welcome, Master > show piggy type : broadcast enabled : yes loop : no inputs /root/MediaPlayer/Audio/cryforyou.mp3 /root/MediaPlayer/Audio/FCMK.mp3 output : options instances instance name : default state : playing position : 0.351690 time : 84897960 length : 235052421 rate : 1000 title : 0 chapter : 0 seekable : 1 playlistindex : 1 > ';

$tmp = array (
  'broadcast enabled',
  'loop',
  'inputs',
  'output',
  'options instances instance name',
  'default state',
  'playing position',
  'time',
  'length' ,
  'rate',
  'title',
  'chapter',
  'seekable',
  'playlistindex'
);

$trans = array();
foreach ($tmp as $x)
{
$trans[$x] = '|'.$x;
}
$newstr = strtr($str, $trans);
$data = explode ('|', $newstr);
unset ($data[0]);
echo join ('<br>', $data);
?>

Link to comment
Share on other sites

Barand,

 

Impecable answer bud, worked a charm, but there is a slight problem, the long line from the output

is for ever changing, the track lines, time left, time remaining etc...

 

Is there an easy way to match like you have done already, basically at the bottom of the function

i have a line which is return $output; ive transfered that to a global to be made available outside the function

but just tried something like this and it didnt work..

 

$str = '$output';

 

the out should pretty much match the array as there all the outputs available

but the inputs part will be longer depending on the amount of tracks in the

playlist....

 

any ideas to make that $str able to accept an ever changing data array from the return

function $output

 

Many thanks for your reply

 

Alan

Link to comment
Share on other sites

Barand,

 

ok im getting there that works great

im trying so hard to understand how you have written your code for me

do have a further question about the parsing of that output...

 

basically currently, anything which doesnt have an output value to output

it basically sticks them all on one line....

 

loop : no 
inputs /root/MediaPlayer/Audio/FCNK.mp3 
output : options instances instance name : default state : playing position : 0.294213 
time : 1372186123 
length : 4653852562 
rate : 1000 
title : 0 
chapter : 0 
seekable : 1 
playlistindex : 1 > 

 

is there any way to not show the oens which dont return a value???

so for instance these dont have values...

 

output : options instances instance name : default state :

 

Many thakns for replying

 

Alan

 

 

Link to comment
Share on other sites

hi there barand,

 

looks like ive solved that one, the array didnt match exactly what the output

was returning... it should of looked liek this the array..

 

$tmp = array (
          'broadcast',
          'loop',
          'inputs',
          'output',
          'options',     
          'instances',       
          'instance',
          'name',
          'state',
          'position',        
          'time',
          'length' ,
          'rate',
          'title',
          'chapter',
          'seekable',
          'playlistindex'  
);

 

whihc is great i have my nice little list now

just some further enhancements for it...

 

current output looks like this...

 

broadcast enabled : yes 
loop : yes 
inputs /root/MediaPlayer/Audio/FCNK.mp3 
output : 
options 
instances 
instance 
name : default 
state : playing 
position : 0.488269 
time : 2277381225 
length : 4653852562 
rate : 1000 
title : 0 
chapter : 0 
seekable : 1 
playlistindex : 1 >

 

as you can see not all fields in that array have : after them

is there an easy to add them to the ones which dont have them

also the > at the end aswell i need to get rid of that as thats part fo the telnet

command for connecting etc and running the command using fsockopen

 

your helps been brillirant :-)

 

Alan

Link to comment
Share on other sites

try

<?php
function my_parse($str)
{
$tmp = array (
	  'broadcast enabled',
	  'loop',
	  'inputs',
	  'output',
	  'options instances instance name',
	  'default state',
	  'playing position',
	  'time',
	  'length' ,
	  'rate',
	  'title',
	  'chapter',
	  'seekable',
	  'playlistindex'
);

$trans = array();
foreach ($tmp as $x)
{
	$trans[$x] = '|'.$x;
}
/**
* "inputs" is a special case as there is no ":", so put one in.
*/
$trans['inputs'] = '|inputs : ';

$newstr = strtr($str, $trans);        // puts "|" before each item keyword so we can split the string into item:value pairs
$data = explode ('|', $newstr);       // puts each pair as an array element
unset ($data[0]);                     // remove unwanted first item

$parseResult = array();
foreach ($data as $itempair)
{
	list ($item, $value) = explode(':', $itempair);
	$value = trim($value, ' >');
	if ($value) $parseResult[] = "$item : $value";       // remove valueless items
}
return join ('<br/>', $parseResult);
}

$str = 'Password: ÿûÿü Welcome, Master > show piggy type : broadcast enabled : yes loop : no inputs /root/MediaPlayer/Audio/cryforyou.mp3 /root/MediaPlayer/Audio/FCMK.mp3 output : options instances instance name : default state : playing position : 0.351690 time : 84897960 length : 235052421 rate : 1000 title : 0 chapter : 0 seekable : 1 playlistindex : 1 > ';

$parsedStr = my_parse($str);
echo $parsedStr;
?>

Link to comment
Share on other sites

Barand,

 

you really know your php bud :-)

works so well, i do have one more question though :-)

 

again its just tarting up the output

 

i have the following now for inputs, its more than one track

 

loop : yes 
inputs : /root/MediaPlayer/Audio/FCNK.mp3 /root/MediaPlayer/Audio/cryforyou.mp3 
output : 

 

how can i get the current function that you have written to do the followign for me

im sorry to ask so many question im just a little limited in functions and arrays :-(

learnign though...

 

loop : yes 
inputs : 
/root/MediaPlayer/Audio/FCNK.mp3 
/root/MediaPlayer/Audio/cryforyou.mp3 
output : 

 

basically for every new track its a new line

or maybe like this....

 

loop : yes 
inputs : /root/MediaPlayer/Audio/FCNK.mp3 
           /root/MediaPlayer/Audio/cryforyou.mp3 
output : 

 

so its kind of tabbed out etc, any further help is very much appreciated

 

Alan

 

 

Link to comment
Share on other sites

You can do this, but IMO it would be better to return an HTML table (much neater)

 

<?php
function my_parse($str)
{
$tmp = array (
          'broadcast',
          'loop',
          'inputs',
          'output',
          'options',     
          'instances',       
          'instance',
          'name',
          'state',
          'position',        
          'time',
          'length' ,
          'rate',
          'title',
          'chapter',
          'seekable',
          'playlistindex' 
);

$trans = array();
foreach ($tmp as $x)
{
	$trans[$x] = '|'.$x;
}
/**
* "inputs" is a special case as there is no ":", so put one in.
*/
$trans['inputs'] = '|inputs : ';
$trans['options'] = '|options : ';
$trans['instances'] = '|instances : ';
$trans['instance'] = '|instance : ';

$newstr = strtr($str, $trans);        // puts "|" before each item keyword so we can split the string into item:value pairs
$data = explode ('|', $newstr);       // puts each pair as an array element
unset ($data[0]);                     // remove unwanted first item

$parseResult = array();
foreach ($data as $itempair)
{
	list ($item, $value) = explode(':', $itempair);
	$value = trim($value, ' >');
	// split multiple items into separate lines
	$value = str_replace (' ', '<br/>           ', $value);         
	if ($value) $parseResult[] = "<strong>$item :</strong> $value";       // remove valueless items
}
return join ('<br/>', $parseResult);
}

$str = 'Password: ÿûÿü Welcome, Master > show piggy type : broadcast enabled : yes loop : no inputs /root/MediaPlayer/Audio/cryforyou.mp3 /root/MediaPlayer/Audio/FCMK.mp3 output : options instances instance name : default state : playing position : 0.351690 time : 84897960 length : 235052421 rate : 1000 title : 0 chapter : 0 seekable : 1 playlistindex : 1 > ';

$parsedStr = my_parse($str);
echo $parsedStr;
?>

Link to comment
Share on other sites

Version IV (table result)

 

function my_parse($str)
{
$tmp = array (
          'broadcast',
          'loop',
          'inputs',
          'output',
          'options',     
          'instances',       
          'instance',
          'name',
          'state',
          'position',        
          'time',
          'length' ,
          'rate',
          'title',
          'chapter',
          'seekable',
          'playlistindex' 
);

$trans = array();
foreach ($tmp as $x)
{
	$trans[$x] = '|'.$x;
}
/**
* "inputs" is a special case as there is no ":", so put one in.
*/
$trans['inputs'] = '|inputs : ';
$trans['options'] = '|options : ';
$trans['instances'] = '|instances : ';
$trans['instance'] = '|instance : ';

$newstr = strtr($str, $trans);        // puts "|" before each item keyword so we can split the string into item:value pairs
$data = explode ('|', $newstr);       // puts each pair as an array element
unset ($data[0]);                     // remove unwanted first item


$resultStr = "<table border='0' cellpadding='2'>";
foreach ($data as $itempair)
{
	list ($item, $value) = explode(':', $itempair);
	$value = trim($value, ' >');
	// split multiple items into separate lines
	$value = str_replace (' ', '<br/>', $value);         
	if ($value) $resultStr .= sprintf("<tr valign='top'><td><strong>%s</strong></td><td>%s</td></tr>", $item, $value);       // remove valueless items
}
$resultStr .= '</table>';
return $resultStr;
}

Link to comment
Share on other sites

Barand,

 

The first option works but with an unexpected distance between the 2 tracks etc

and the tabled version didnt work for me, but going to try it again and see if i can

get it working i didnt check the errors so willtry that now

 

heres the output from the first version

 

http://www.cr-isp.com/vlcremote/show.php?com=show&playlist=piggy

 

Alan

 

again thanks fro yor help, im definately getting there

 

Alan

Link to comment
Share on other sites

Not sure what you mean. The HTML output that I get is

<table border='0' cellpadding='2'><tr valign='top'><td><strong>broadcast enabled </strong></td><td>yes</td></tr>
<tr valign='top'><td><strong>loop </strong></td><td>no</td></tr>
<tr valign='top'><td><strong>inputs </strong></td><td>/root/MediaPlayer/Audio/cryforyou.mp3<br/>/root/MediaPlayer/Audio/FCMK.mp3</td></tr>
<tr valign='top'><td><strong>name </strong></td><td>default</td></tr>
<tr valign='top'><td><strong>state </strong></td><td>playing</td></tr>
<tr valign='top'><td><strong>position </strong></td><td>0.351690</td></tr>
<tr valign='top'><td><strong>time </strong></td><td>84897960</td></tr>
<tr valign='top'><td><strong>length </strong></td><td>235052421</td></tr>
<tr valign='top'><td><strong>rate </strong></td><td>1000</td></tr>
<tr valign='top'><td><strong>seekable </strong></td><td>1</td></tr>
<tr valign='top'><td><strong>playlistindex </strong></td><td>1</td></tr>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.