php如何获得指定值并且组成新数组
有一个php的ping程序目的是来测试网速和连接情况的,程序和运行的结果如下,现在想把结果取出部分值并组成新数组,如下面的结果,要取得值"50%los","232ms",...
有一个php的ping程序目的是来测试网速和连接情况的,程序和运行的结果如下,现在想把结果取出部分值并组成新数组,如下面的结果,要取得值"50% los","232ms","232ms"."232ms"返回新数组如:"50%,232,232,232",也就是程序运行结果直接返回"50%,232,232,232",而不要那么多ping 程序执行结果
////////ping 程序开始////////
<FORM method='POST' action='<?php echo $PHP_SELF;?>'>
<table border=1 cellspacing=0 cellpadding=1 width="400" bgcolor=#808080>
<tr>
<th colspan=2 align=center>怎么利用PHP去ping一个地址</th>
</tr>
<tr>
<th bgcolor=#066b98>请输入url或ip地址:</th>
<td><INPUT type='text' name='server' size='30' maxlength='30'></td>
</tr>
<tr>
<td></td>
<td><INPUT type='submit' value='发送' ></td>
</tr>
</table>
</FORM>
<?php
if(($server == '127.0.0.1')||($server == 'localhost')||($server == getenv("SERVER_ADDR"))) {
echo "<HR noshade size=1<br>";
echo "<font color=red size=1>Fuck Off!</font><br>";
echo "<HR noshade size=1<br>";
exit;
} else {
if($server =="") {
echo "<H2>请输入url或ip地址:</H2>";
exit;
}
$ip1 = getenv("REMOTE_ADDR");
$hname = getenv("HTTP_HOST");
$count = 5;
$psize = 32;
echo "<b>Your IP is:</b> <i>$ip1</i><br>"
."<b>Trying to ping:</b> <i>$server</i><br>"
."<b>Using server:</b> <i>$hname</i>";
echo "<HR noshade size=1<br>";
echo "<b>STATS:</b><br><br>";
$command = "ping -n $count -l $psize $server";
exec($command, $result, $rval);
if(count($result) <= 0) {
echo "<font color=red>没有响应</font><br>";
echo "<HR noshade size=1>";
exit;
}
for ($i = 0; $i < count($result); $i++) {
echo "<font color=red>$result[$i]</font><br>";
}
echo "<HR noshade size=1>";
}
?>
////////ping 程序结束////////
////////ping 程序执行结果////////
Pinging baidu.com [202.108.23.59] with 32 bytes of dat
Request timed out.
Request timed out.
Reply from 202.108.23.59: bytes=32 time=232ms TTL=243
Reply from 202.108.23.59: bytes=32 time=232ms TTL=243
Ping statistics for 202.108.23.59:
Packets: Sent = 4, Received = 2, Lost = 2 (50% los)
Approximate round trip times in milli-seconds:
Minimum = 232ms, Maximum = 232ms, Average = 232ms 展开
////////ping 程序开始////////
<FORM method='POST' action='<?php echo $PHP_SELF;?>'>
<table border=1 cellspacing=0 cellpadding=1 width="400" bgcolor=#808080>
<tr>
<th colspan=2 align=center>怎么利用PHP去ping一个地址</th>
</tr>
<tr>
<th bgcolor=#066b98>请输入url或ip地址:</th>
<td><INPUT type='text' name='server' size='30' maxlength='30'></td>
</tr>
<tr>
<td></td>
<td><INPUT type='submit' value='发送' ></td>
</tr>
</table>
</FORM>
<?php
if(($server == '127.0.0.1')||($server == 'localhost')||($server == getenv("SERVER_ADDR"))) {
echo "<HR noshade size=1<br>";
echo "<font color=red size=1>Fuck Off!</font><br>";
echo "<HR noshade size=1<br>";
exit;
} else {
if($server =="") {
echo "<H2>请输入url或ip地址:</H2>";
exit;
}
$ip1 = getenv("REMOTE_ADDR");
$hname = getenv("HTTP_HOST");
$count = 5;
$psize = 32;
echo "<b>Your IP is:</b> <i>$ip1</i><br>"
."<b>Trying to ping:</b> <i>$server</i><br>"
."<b>Using server:</b> <i>$hname</i>";
echo "<HR noshade size=1<br>";
echo "<b>STATS:</b><br><br>";
$command = "ping -n $count -l $psize $server";
exec($command, $result, $rval);
if(count($result) <= 0) {
echo "<font color=red>没有响应</font><br>";
echo "<HR noshade size=1>";
exit;
}
for ($i = 0; $i < count($result); $i++) {
echo "<font color=red>$result[$i]</font><br>";
}
echo "<HR noshade size=1>";
}
?>
////////ping 程序结束////////
////////ping 程序执行结果////////
Pinging baidu.com [202.108.23.59] with 32 bytes of dat
Request timed out.
Request timed out.
Reply from 202.108.23.59: bytes=32 time=232ms TTL=243
Reply from 202.108.23.59: bytes=32 time=232ms TTL=243
Ping statistics for 202.108.23.59:
Packets: Sent = 4, Received = 2, Lost = 2 (50% los)
Approximate round trip times in milli-seconds:
Minimum = 232ms, Maximum = 232ms, Average = 232ms 展开
2个回答
展开全部
<?php
//有一个php的ping程序目的是来测试网速和连接情况的,程序和运行的结果如下,现在想把结果取出部分值并组成新数组,如下面的结果,要取得值"50% los","232ms","232ms"."232ms"返回新数组如:"50%,232,232,232",也就是程序运行结果直接返回"50%,232,232,232",而不要那么多ping 程序执行结果
////////ping 程序开始////////?>
<FORM method='POST' action='<?php echo $PHP_SELF;?>'>
<table border=1 cellspacing=0 cellpadding=1 width="400" bgcolor=#808080>
<tr>
<th colspan=2 align=center>怎么利用PHP去ping一个地址</th>
</tr>
<tr>
<th bgcolor=#066b98>请输入url或ip地址:</th>
<td><INPUT type='text' name='server' size='30' maxlength='30'></td>
</tr>
<tr>
<td></td>
<td><INPUT type='submit' value='发送' ></td>
</tr>
</table>
</FORM>
<?php
if(($server == '127.0.0.1')||($server == 'localhost')||($server == getenv("SERVER_ADDR"))) {
echo "<HR noshade size=1<br>";
echo "<font color=red size=1>Fuck Off!</font><br>";
echo "<HR noshade size=1<br>";
exit;
} else {
if($server =="") {
echo "<H2>请输入url或ip地址:</H2>";
exit;
}
}
$ip1 = getenv("REMOTE_ADDR");
$hname = getenv("HTTP_HOST");
$count = 5;
$psize = 32;
echo "<b>Your IP is:</b> <i>$ip1</i><br>"
."<b>Trying to ping:</b> <i>$server</i><br>"
."<b>Using server:</b> <i>$hname</i>";
echo "<HR noshade size=1<br>";
echo "<b>STATS:</b><br><br>";
// 该函数返回对提交主机ping的值 lost/min/max/avg
function myPing($ping_count='4',$ping_psize='32', $ping_server='www.baidu.com')
{
$command = "ping -n $ping_count -l $ping_psize $ping_server";
exec($command, $result, $rval);
$aa=count($result);
if(count($result) <= 0) {
$myPing='100%,0,0,0';
exit;
}
$bb=$aa-3;
$cc=$aa-1;
$ping_los=explode(',',$result[$bb]);
$ping_times=explode(',',$result[$cc]);
$ping_lose_value= strstr($ping_los[2],'(');
$ping_lose_value=preg_replace('/[^0-9.]+/','',$ping_lose_value);
$ping_times_min= preg_replace('/[^0-9.]+/','',$ping_times[0]);
$ping_times_max= preg_replace('/[^0-9.]+/','',$ping_times[1]);
$ping_times_avg= preg_replace('/[^0-9.]+/','',$ping_times[2]);
$myPing= $ping_lose_value.'%,'.$ping_times_min.','.$ping_times_max.','.$ping_times_avg ;
echo $myPing;
}
echo myPing(6,32,$server);
//2个小时...郁闷 本以为三十分可以解决 函数已经给你封装了 你可以自己扩展了 希望尽可能的还是多给点分 好累..
?>
//有一个php的ping程序目的是来测试网速和连接情况的,程序和运行的结果如下,现在想把结果取出部分值并组成新数组,如下面的结果,要取得值"50% los","232ms","232ms"."232ms"返回新数组如:"50%,232,232,232",也就是程序运行结果直接返回"50%,232,232,232",而不要那么多ping 程序执行结果
////////ping 程序开始////////?>
<FORM method='POST' action='<?php echo $PHP_SELF;?>'>
<table border=1 cellspacing=0 cellpadding=1 width="400" bgcolor=#808080>
<tr>
<th colspan=2 align=center>怎么利用PHP去ping一个地址</th>
</tr>
<tr>
<th bgcolor=#066b98>请输入url或ip地址:</th>
<td><INPUT type='text' name='server' size='30' maxlength='30'></td>
</tr>
<tr>
<td></td>
<td><INPUT type='submit' value='发送' ></td>
</tr>
</table>
</FORM>
<?php
if(($server == '127.0.0.1')||($server == 'localhost')||($server == getenv("SERVER_ADDR"))) {
echo "<HR noshade size=1<br>";
echo "<font color=red size=1>Fuck Off!</font><br>";
echo "<HR noshade size=1<br>";
exit;
} else {
if($server =="") {
echo "<H2>请输入url或ip地址:</H2>";
exit;
}
}
$ip1 = getenv("REMOTE_ADDR");
$hname = getenv("HTTP_HOST");
$count = 5;
$psize = 32;
echo "<b>Your IP is:</b> <i>$ip1</i><br>"
."<b>Trying to ping:</b> <i>$server</i><br>"
."<b>Using server:</b> <i>$hname</i>";
echo "<HR noshade size=1<br>";
echo "<b>STATS:</b><br><br>";
// 该函数返回对提交主机ping的值 lost/min/max/avg
function myPing($ping_count='4',$ping_psize='32', $ping_server='www.baidu.com')
{
$command = "ping -n $ping_count -l $ping_psize $ping_server";
exec($command, $result, $rval);
$aa=count($result);
if(count($result) <= 0) {
$myPing='100%,0,0,0';
exit;
}
$bb=$aa-3;
$cc=$aa-1;
$ping_los=explode(',',$result[$bb]);
$ping_times=explode(',',$result[$cc]);
$ping_lose_value= strstr($ping_los[2],'(');
$ping_lose_value=preg_replace('/[^0-9.]+/','',$ping_lose_value);
$ping_times_min= preg_replace('/[^0-9.]+/','',$ping_times[0]);
$ping_times_max= preg_replace('/[^0-9.]+/','',$ping_times[1]);
$ping_times_avg= preg_replace('/[^0-9.]+/','',$ping_times[2]);
$myPing= $ping_lose_value.'%,'.$ping_times_min.','.$ping_times_max.','.$ping_times_avg ;
echo $myPing;
}
echo myPing(6,32,$server);
//2个小时...郁闷 本以为三十分可以解决 函数已经给你封装了 你可以自己扩展了 希望尽可能的还是多给点分 好累..
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询