Question

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.

Was it helpful?

Solution

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top