gpt4 book ai didi

php - php 计数函数中的错误计数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:53 26 4
gpt4 key购买 nike

$checkvpn = trim(`sudo ifconfig | grep -i "inet addr:" | cut -d ":" -f2 | awk '{print $1}'`);
$checkvpnstring = (string)$checkvpn;
$checkvpncount = count("\n",$checkvpnstring);
$checkvpncount++;
$checkvpnexplode = explode("\n", $checkvpnstring);

for($i=0;$i<=$checkvpncount;$i++){
$ip = $checkvpnexplode[$i];
if($ip == $externalip){
$vpnconfigured = "yes";
break;
}
}

我有上面的代码,其中我获得了我的 linux 机器上可用的所有 ip。但是当我有 13 个 ip 并且只计数 3 并且如果我回显 $checkvpnexplode[12] 然后它打印第 13 个 ip 地址时,就会出现问题。

我的问题是,为什么 count 函数只计算 3 而 explode 函数给出所有的输出。
我对 explode 和计数使用了相同的函数。

$checkvpn 的值 = 10.10.1.1 10.10.2.1 103.29.235.45 127.0.0.1

value of $checkvpnstring = 10.10.1.1 10.10.0.1 10.10.8.1 103.29.235.15 10.10.10.1 127.0.0.1 10.10.12.1 10.10.9.1 10.10.11.1 10.10.7.1 10.10.5.1 10.10.2.1 10.10.6.1 10.10.3.1 10.10.4.1

value of $checkvpnstring = 10.10.1.1 10.10.0.1 10.10.8.1 103.29.235.15 10.10.10.1 127.0.0.1 10.10.12.1 10.10.9.1 10.10.11.1 10.10.7.1 10.10.5.1 10.10.2.1 10.10.6.1 10.10.3.1 10.10.4.1

最佳答案

$checkvpncount = count("\n",$checkvpnstring); 是错误的;应该是 $checkvpncount = count($checkvpnstring); 它看起来像是你的 explode 语句中的复制粘贴错误

我已经测试了你的代码并做了一些小改动:

<?
$vpnconfigured = "no";
$externalip = "1.2.3.4";
$checkvpn = trim(`sudo ifconfig | grep -i "inet addr:" | cut -d ":" -f2 | awk '{print $1}'`);
$checkvpnstring = (string)$checkvpn;
$checkvpncount = count($checkvpnstring);
$checkvpncount++;
$checkvpnexplode = explode("\n", $checkvpnstring);

for($i=0;$i<=$checkvpncount;$i++){
$ip = $checkvpnexplode[$i];
if($ip == $externalip){
$vpnconfigured = "yes";
break;
}
}
var_dump($checkvpncount);
var_dump($checkvpnexplode);
var_dump($checkvpn);
echo $vpnconfigured;
?>

看来你的计数被额外的参数搞糊涂了;

输出是:

int(2)
array(2) {
[0]=>
string(13) "192.168.1.240"
[1]=>
string(9) "127.0.0.1"
}
string(23) "192.168.1.240
127.0.0.1"

和我相应的 ifconfig 输出:

eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
inet addr:192.168.1.240 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: xxxx::xxxx:xxxx:xxxx:xxxx/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6501443 errors:0 dropped:7 overruns:0 frame:21
TX packets:7860026 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2026553110 (1.8 GiB) TX bytes:5197150028 (4.8 GiB)
Interrupt:18

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:146814 errors:0 dropped:0 overruns:0 frame:0
TX packets:146814 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:25834701 (24.6 MiB) TX bytes:25834701 (24.6 MiB)

关于php - php 计数函数中的错误计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33346839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com