note 86098 added to function.preg-match

From: Date: Fri, 03 Oct 2008 01:23:45 +0000
Subject: note 86098 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-145002@lists.php.net to get a copy of this message
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 ---- Server IP: 89.190.194.18 Probable Submitter: 85.91.149.4 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/86098 Del: integrated -- https://master.php.net/note/delete/86098/integrated Del: useless -- https://master.php.net/note/delete/86098/useless Del: bad code -- https://master.php.net/note/delete/86098/bad+code Del: spam -- https://master.php.net/note/delete/86098/spam Del: non-english -- https://master.php.net/note/delete/86098/non-english Del: in docs -- https://master.php.net/note/delete/86098/in+docs Del: other reasons-- https://master.php.net/note/delete/86098 Reject -- https://master.php.net/note/reject/86098 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145002) next »