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.
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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.
Here is an another solution:
preg_match('/RX packets:(\d+)/', $strWlan0, $result);
$strWlan0 = $result[1];
.bonga
Thanks for that ! I’ll give it a shot asap !
This worked brilliantly for me, thanks !
Knew there was a better way of doing it, I just couldn’t puzzle it out using Regexp haha 😀
Thanks for the tip.
You’re welcome. But I’ve made a mistake in my previous post. Can you exchange the “blockquote” in my previous post to “code” element? It would be better for the visitors of your blog. Thank you in advance.
Done 🙂