Php: obtain the string between two strings in a string. br-lan Link encap: Ethernet HWaddr F0: B4: 29: 55: 6C: 2E
Inet addr: 192.168.8.9 Bcast: 192.168.8.255 Mask: 255.255.255.0
Inet6 addr: fdc1: b4aa: 57ba: 1/60 Scope: Global
Inet6 addr: fe80: f2b4: 29ff: fe55: 6c2e/64 Scope: Link
Inet6 addr: fd7b: 7c0f: 5360: 4: 1/62 Scope: Global
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 6135 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 7308 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX bytes: 774045 (755.9 KiB) TX bytes: 2084983 (1.9 MiB)
// In the above string, I want to retrieve the IP address between inet addr: 192.168.8.9 Bcast. how can I write the php regular expression? Thank you.
// $ Return_array is the preceding string array.
foreach ( $return_array as $value ){//if ( preg_match( "/inet addr:/i", $value, $temp_array) )if ( preg_match("\binet addr:\b.*\bBcast\b", $value, $temp_array) ){echo '
';print_r($temp_array);//var_dump( $temp_array );}}
Reply to discussion (solution)
Up, online, etc.
If (preg_match ("[0-9] {1, 3} [.] [0-9] {1, 3} [.] [0-9] {1, 3} [.] [0-9] {1, 3} ", $ value, $ temp_array) // in the code, this line has an error. use a tool to test OK. {echo'
'; // Print_r ($ temp_array); var_dump ($ temp_array );}
// \b[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}\b//if (preg_match("/\b[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}\b/", $value, $temp_array))if (preg_match("/[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}/", $value, $temp_array)) // OK{echo '
';//print_r($temp_array);var_dump( $temp_array );}
if ( preg_match("/inet addr:(.+?)\s+Bcast:/", $value, $temp_array) ) { echo '
'; print_r($temp_array); //var_dump( $temp_array ); }
$s =<<< TXTbr-lan Link encap:Ethernet HWaddr F0:B4:29:55:6C:2E inet addr:192.168.8.9 Bcast:192.168.8.255 Mask:255.255.255.0 inet6 addr: fdc1:b4aa:57ba::1/60 Scope:Global inet6 addr: fe80::f2b4:29ff:fe55:6c2e/64 Scope:Link inet6 addr: fd7b:7c0f:5360:4::1/62 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6135 errors:0 dropped:0 overruns:0 frame:0 TX packets:7308 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:774045 (755.9 KiB) TX bytes:2084983 (1.9 MiB)TXT;preg_match('/(?<=addr:)[.\d]+/', $s, $m);echo $m[0];
192.168.8.9