Php: obtains the string between two strings in a string.

Source: Internet
Author: User
Tags php regular expression
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.