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. 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? Thanks.

有帮助吗?

解决方案

This regular expression could be improved but it will do the trick.

If your output is in $lines then you can use

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

Then the first match will be stored in

$matches[1]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top