Jan 282013

I’ve been doing some coding for my Raspberry Pi’s Wireless Network Configuration page, and was trying to find a good way to get just a single number out of the block that ifconfig outputs –
I wanted to get the number after RX packets: in this example.

RX packets:2383877 errors:0 dropped:8221 overruns:0 frame:0
TX packets:2492088 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:734455593 (700.4 MiB) TX bytes:3075500605 (2.8 GiB)

I ended up writing a oneliner to do it for me, and put it into a function so I could use it more.
$strWlan0 is the block of code above.

substr($strWlan0,strpos($strWlan0,"RX packets:")+11,strpos(substr($strWlan0,strpos($strWlan0,"RX packets:")+11)," "));

If anyone has a better way of doing it though, please add it in the comments.


  5 Responses to “PHP Snippet : Parsing out an individual string”

  1. Here is an another solution:

    preg_match('/RX packets:(\d+)/', $strWlan0, $result);
    $strWlan0 = $result[1];


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>