简体   繁体   English

nslookup /主机解析PHP

[英]nslookup/host parsing PHP

I'm trying to resolve a hostname in PHP and can't use the builtin gethostbyname function because it doesn't support a timeout option, therefore i'm trying to deal with that problem by looking up the hostname with nslookup or host . 我试图在PHP中解析一个主机名,并且不能使用内置的gethostbyname函数,因为它不支持超时选项,因此我试图通过使用nslookuphost查找主机名来解决该问题。 The Problem is that these Functions/Programs don't return the plain result but something like 问题是这些函数/程序不会返回简单的结果,而是类似

Non-authoritative answer:
Name:   google.de
Address: 173.194.35.159
Name:   google.de
Address: 173.194.35.151
Name:   google.de
Address: 173.194.35.152

Does anybody know a fast function in PHP to parse that so it just returns the first IP Adress? 有谁知道PHP中有一个快速函数来解析它,因此它只返回第一个IP地址? Thanks. 谢谢。

This regular expression could be improved but it will do the trick. 此正则表达式可以改进,但可以解决问题。

If your output is in $lines then you can use 如果您的输出为$ lines,则可以使用

preg_match('/Address: ([0-9\.]*).*/', $lines, $matches);

Then the first match will be stored in 然后第一个匹配项将存储在

$matches[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM