I am encountering a problem with string formatting while trying to get only the names of currently configured network interfaces on a Linux Machine.
1. <?php
2. $temp = shell_exec("/sbin/ifconfig | cut -b 1-10");
3. echo $temp; //Outputs: eth0 lo
4. $arr = explode(" ",$temp);
5. echo "First Location:".$arr[0]; //Outputs: eth0
6. echo "Second Location:".$arr[1]; //Outputs:
7. echo count($a); //Outputs: 165
8. ?>
How can i get $arr of size=2 such that echo $arr[0]; //gives 'eth0' echo $arr[1]; //gives 'lo'
Thanks a lot
Update: I thing following command will do the magic for me
ifconfig | grep -o -e "[a-z][a-z]*[0-9]*[ ]*Link" | perl -pe "s|^([a-z]*[0-9]*)[ ]*Link|\1|"
but i am doing something wrong while executing it from a php file because the browser does not show anything.
<?php
$temp = shell_exec("ifconfig | grep -o -e \"[a-z][a-z]*[0-9]*[ ]*Link\" | perl -pe \"s|^([a-z]*[0-9]*)[ ]*Link|\\1|\"");
echo $temp;
?>