Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parse Host+IP to IP Address only [closed]

Tags:

regex

php

I'm trying to get this: 46-121-31-23.example.net

To be parsed to this: 46.121.31.23

Replacing the hyphens and removing the characters using REGEX wouldn't be enough, since the result of this would be 46.121.31.23..

like image 423
Lior Avatar asked Aug 07 '26 15:08

Lior


1 Answers

How about gethostbyname()? While your particular hostname has the IPv4 address encoded into its 'friendly' name, there's no guarantee that will always be true. So use the real name->ip lookup system: DNS

$ip = gethostbyname('46-121-31-23.example.net');
echo $ip; // 46.121.31.23

comment followup: That hostname obviously doesn't exist:

marc@panic:~$ host -t ns static.012.net.il             
static.012.net.il name server pdns.goldenlines.net.il.
static.012.net.il name server sdns.goldenlines.net.il.

marc@panic:~$ host 46-121-31-23.static.012.net.il pdns.goldenlines.net.il   
Using domain server:
Name: pdns.goldenlines.net.il
Address: 212.117.129.3#53
Aliases: 

Host 46-121-31-23.static.012.net.il not found: 3(NXDOMAIN)

hence there's no way to do a DNS lookup, since the servers which are authoritative for that domain have no idea what you're talking about.

However, the reverse mapping (IP->Hostname) DOES work:

marc@panic:~$ host 46.121.31.23
23.31.121.46.in-addr.arpa domain name pointer 46-121-31-23.static.012.net.il.

So for some reason, that provider's only doing reverse mappings, and not forward.

like image 61
Marc B Avatar answered Aug 10 '26 07:08

Marc B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!