note 86098 deleted from function.preg-match by cmb
| From: | cmb@php.net | Date: | Sun, 04 Dec 2016 12:04:43 +0000 |
| Subject: | note 86098 deleted from function.preg-match by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208178@lists.php.net to get a copy of this message | ||
Note Submitter: Steve Todorov
----
While I was reading the preg_match documentation I didn't found how to match an IP..
Let's say you need to make a script that is working with ip/host and you want to show the
hostname - not the IP.
Well this is the way to go:
<?php
/* This is an ip that is "GET"/"POST" from somewhere */
$ip = $_POST['ipOrHost'];
if(preg_match('/(\d+).(\d+).(\d+).(\d+)/',$ip))
$host = gethostbyaddr($ip);
else
$host = gethostbyname($ip);
echo $host;
?>
This is a really simple script made for beginners !
If you'd like you could add restriction to the numbers.
The code above will accept all kind of numbers and we know that IP address could be MAX
255.255.255.255 and the example accepts to 999.999.999.999.
Wish you luck!
Best wishes,
Steve