简体   繁体   中英

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 . 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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