4
回答
寻找PHP字符串处理的更简洁的方法...


<?php
	/*
	PING www.myluoluo.com (122.49.12.39) 32(60) bytes of data.
	40 bytes from 122.49.12.39: icmp_seq=1 ttl=50 time=316 ms
	40 bytes from 122.49.12.39: icmp_seq=3 ttl=50 time=317 ms
	40 bytes from 122.49.12.39: icmp_seq=4 ttl=50 time=318 ms


	--- www.myluoluo.com ping statistics ---
	4 packets transmitted, 3 received, 25% packet loss, time 4399ms
	rtt min/avg/max/mdev = 316.859/317.583/318.339/0.888 ms
	PING 111.111.111.111 (111.111.111.111) 32(60) bytes of data.


	--- 111.111.111.111 ping statistics ---
	4 packets transmitted, 0 received, 100% packet loss, time 12999ms
	*/
	$ip="111.111.111.111";
	exec("ping -c 4 -s 32 $ip",$arr);
 	foreach ($arr as $str) {
		//去掉空格
		$str = str_replace(" ","",$str);
		//查找received,的位置,+9为同时将该关键字包括
		$tmp = $str = substr($str,strpos($str,"received")+9);
		//查找%后面的,并存入str
		$str = substr($str,strpos($str,"%"));
		//将上面查找到的字符串从tmp中移除
		$str = str_replace(substr($str,strpos($str,"%")),"",$tmp);
		echo $str;
	}
?>


本意是,取得100% packet loss中的百分比,用来做Ping监控...
刚刚接触PHP,由于.Net不能执行在Linux下,只能看一点写一点,结果写完发现代码太囊肿了..
不知道各位看官有没有更简洁的方法,如果有的话..天冷了,活动下双手吧 :)
然后是...O(∩_∩)O谢谢

PHP
举报
顶部