Home >Backend Development >PHP Tutorial >php, 在一串字符中中取得两个字符串之间的字符串

php, 在一串字符中中取得两个字符串之间的字符串

WBOY
WBOYOriginal
2016-06-23 13:20:59949browse

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)

//在以上字符串中,我要取出inet addr:192.168.8.9  Bcast之间的ip地址,请问php的正刚表达式怎么写?谢谢。
// $return_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 '<br /><br />';			print_r($temp_array);			//var_dump( $temp_array );		}	}


回复讨论(解决方案)

up,在线等。

		if (preg_match("[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}", $value, $temp_array)) // 放代码里,这行有错误,用工具测试ok.		{			echo '<br /><br />';			//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 '<br /><br />';			//print_r($temp_array);			var_dump( $temp_array );		}

if ( preg_match("/inet addr:(.+?)\s+Bcast:/", $value, $temp_array) )    {        echo '<br /><br />';        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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn