在阵列DNS条目中搜索



我看到了很多答案,但我就是无法让它发挥作用。

我想检查数组中是否有(部分(值。

//Get DNS records
$result = dns_get_record("php.net", DNS_ALL);
print_r($result);
//If the value php-smtp3.php.net is found, echo it
if (in_array("php-smtp3.php.net", $result   )) {
echo "Found!";
}

添加:json_encoded$result,来自我的网络

[
{
"host"  : "php.net" ,
"class" : "IN" ,
"ttl"   : 375 ,
"type"  : "A" ,
"ip"    : "208.43.231.9"
} ,
{
"host"   : "php.net" ,
"class"  : "IN" ,
"ttl"    : 375 ,
"type"   : "NS" ,
"target" : "dns2.easydns.net"
} 
]

非常感谢大家,我想我已经快到了,如果我没有完全理解,我很抱歉。这就是我现在拥有的:

$result = dns_get_record("php.net", DNS_ALL);
print_r($result);
$result = json_decode($result, true);
$result = array_filter($result, function($x) {
return in_array("smtp", $x, true);
//If in the array, no matter where, is "smtp" then echo "found" is what i am trying to achieve
echo "<h1>FOUND</h1>";
});

更新:

$result = dns_get_record("php.net", DNS_ALL);
$result = json_decode($data, true);

function process($data) {
foreach ($data as $key => $value) {
if (is_array($value)) {
return process($value);
}
if (is_string($value) && strpos($value,'smtp') !== false) {
echo "FOUND";
return true;
}
}
return false;
}
$result = array_filter($result, 'process');

我正在尝试两种方式。。。很抱歉,我一直在尝试从DNS条目中获取一个简单字符串的响应。这背后的实际想法是:

1( 检查域的DNS记录2( 检查是否有SPF记录3( 如果是,只需说"找到SPF记录">

$values = array_reduce(
dns_get_record("php.net", DNS_ALL),
function ($out, $item) {
return array_merge($out, array_values($item));
},
[]
);
var_dump(in_array("dns2.easydns.net", $values));
//Result is bool(true)

使用json_decode后,数据返回一个多维数组,其中一些数组也包含一个数组。

如果你想检查一个分部值,那么如果字符串包含一个子字符串,你可以使用strpos,但你必须循环所有的字符串,也在子数组中。

因此,您可以将array_filter与递归方法结合使用。

例如,如果您想查找子字符串smtp3,可以使用:

function process($data) {
foreach ($data as $key => $value) {
if (is_array($value)) {
return process($value);
}
if (is_string($value) && strpos($value,'smtp3') !== false) {
return true;
}
}
return false;
}
$result = array_filter($result, 'process');
print_r($result);

查看php演示

您所需要做的就是使结果变平并搜索一个值,如下所示:

<?php
$values = array_reduce(
dns_get_record("php.net", DNS_ALL),
function ($out, $item) {
return array_merge($out, array_values($item));
},
[]
);
var_dump(in_array("dns2.easydns.net", $values));

最新更新