gpt4 book ai didi

php - strpos 未返回正确的结果

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:32 26 4
gpt4 key购买 nike

我一直在努力反对谷歌试图解决这个问题,但我所能找到的只是那些不明白 strpos 返回找到的字符串的位置的人,所以 0 是一个有效值。这不是我的问题......简而言之:

$services=shell_exec('ps aux | grep udp');
print_r(is_string($services));
print_r($services);

返回:

1
root 833 0.0 0.1 1852 768 ? S 19:25 0:00 daemon -r -n udp-server1 /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root 834 0.0 0.1 1852 768 ? S 19:25 0:00 daemon -r -n udp-server2 /usr/bin/ncd/udp-server.php network:13000:false:false.
root 848 48.6 2.3 37036 11744 ? R 19:25 18:07 /usr/bin/php /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root 849 48.6 2.3 37064 11780 ? R 19:25 18:07 /usr/bin/php /usr/bin/ncd/udp-server.php network:13000:false:false.
www-data 1675 0.0 0.0 1368 444 ? S 20:02 0:00 sh -c ps aux | grep udp
www-data 1677 0.0 0.1 1456 528 ? S 20:02 0:00 grep udp

但这就是我期望的结果:

$statuses=explode("\n",file_get_contents('/var/www/misc/udp-settings.inc'));
print_r($statuses);
foreach($statuses as $status){
print_r(strpos($services,$status)===false);
}

返回:

Array ( [0] => wifi:3333:true:true [1] => network:13000:false:false [2] => )
1
1
1

这显然是错误的,因为字符串绝对存在于干草堆中。我还尝试了 stripos、mb_strpos、mb_stripos 和 preg_match ,结果相似...我已经用 is_string 检查了所有内容,并且全部检查完毕。我还在每个字符串的末尾附加了 '' ,并用双引号括住每个字符串,试图对两个字符串进行类型转换,以便它们匹配(不确定这是否有意义,但我越来越绝望了,哈哈)。

我不知所措,我怀疑可能与 udp-settings.inc 文件的编码方式有关,但我不太确定......我已经没有什么可以尝试的了,真的需要一些帮助,还有其他人遇到过这种类型的问题吗?

编辑:

需要明确的是,如果我将字符串直接复制并粘贴到 php 文件中,我知道这是可行的,但这在生产中不是一个选项。我尝试过的另一件事是爆炸和内爆 $statuses 的值,这给了我相同的结果。

正如下面的评论中提到的,strlen 返回适当的结果,我还使用 mb_detect_encoding 检查了字符串,它们都返回 ASCII

最佳答案

这对我来说很好,尝试一下(唯一真正的区别是状态上的修剪和数组上空条目的验证以及找到/未找到的 if/echo):

Live DEMO.

<?php
$services = <<<DATA
root 833 0.0 0.1 1852 768 ? S 19:25 0:00 daemon -r -n udp-server1 /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root 834 0.0 0.1 1852 768 ? S 19:25 0:00 daemon -r -n udp-server2 /usr/bin/ncd/udp-server.php network:13000:false:false.
root 848 48.6 2.3 37036 11744 ? R 19:25 18:07 /usr/bin/php /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root 849 48.6 2.3 37064 11780 ? R 19:25 18:07 /usr/bin/php /usr/bin/ncd/udp-server.php network:13000:false:false.
www-data 1675 0.0 0.0 1368 444 ? S 20:02 0:00 sh -c ps aux | grep udp
www-data 1677 0.0 0.1 1456 528 ? S 20:02 0:00 grep udp
DATA;

$statuses = <<<DATA
wifi:3333:true:true
network:13000:false:false

DATA;

$status = explode("\n",$statuses);
print_r($status);

foreach($status as $current_status)
{
$new_status = trim($current_status);
if (empty($new_status))
continue;

if (strpos($services,$new_status)!==false)
{
echo $new_status, " was found...\n";
}
else
{
echo $new_status, " was not found...\n";
}
}

您的代码应该如下所示:

<?php
$services = shell_exec('ps aux | grep udp');
$statuses = explode("\n",file_get_contents('/var/www/misc/udp-settings.inc'));
$status = explode("\n",$statuses);
foreach($status as $current_status)
{
$new_status = trim($current_status);
if (empty($new_status))
continue;

if (strpos($services,$new_status)!==false)
{
echo $new_status, " was found...\n";
}
else
{
echo $new_status, " was not found...\n";
}
}

关于php - strpos 未返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764702/

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