Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP validate ip address by using regular expression

PHP validate ip address by using regular expression

This example will show how to validate an IP Address to see if it is valid by using PHP Regular Expression.
First, you may need to know what an IP Address is consist of. An IP Address consists of 4 parts. Each part separated by period “.” and these part consists the digits which ranges from 0 to 255.

And this is a PHP function to validate IP Address by using Regular Expression:

<?php
	function validateIpAddress($ip_addr)
	{
		//first of all the format of the ip address is matched
		if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
		{
			//now all the intger values are separated
			$parts=explode(".",$ip_addr);
			//now we need to check each part can range from 0-255
			foreach($parts as $ip_parts)
			{
				if(intval($ip_parts)>255 || intval($ip_parts)<0)
				return false; //if number is not within range of 0-255
			}
			return true;
		}
		else
			return false; //if format of ip address doesn't matches
	}
?>

<?php function validateIpAddress($ip_addr) { //first of all the format of the ip address is matched if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr)) { //now all the intger values are separated $parts=explode(".",$ip_addr); //now we need to check each part can range from 0-255 foreach($parts as $ip_parts) { if(intval($ip_parts)>255 || intval($ip_parts)<0) return false; //if number is not within range of 0-255 } return true; } else return false; //if format of ip address doesn't matches } ?>

Example:

<?php
	validateIpAddress("192.168.1.99"); // return true
	validateIpAddress("192.168.199"); // return false
	validateIpAddress("Regular Expression"); // return false
?>

<?php validateIpAddress("192.168.1.99"); // return true validateIpAddress("192.168.199"); // return false validateIpAddress("Regular Expression"); // return false ?>

Feb 22, 2011Hoan Huynh
xcode change iphone app display namePHP Read File Using Fread() Simple Example
You Might Also Like:
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • CSharp Validate Email Address Function With Regular Expression
  • Validate URL Use Regular Expression (Regex) In PHP
  • How To Track Website With Multiple Google Analytisc Accounts
  • CSharp Validate Internet URL Function With Regular Expression
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Validate email using regular expression in ASP.NET
  • Get WOEID Of A City Name From IP Address With PHP
  • Get Image Width Height With JQuery And JavaScript
  • Javascript Problem Set focus textbox on Firefox
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago PHPexplode, ip address, preg_match, regular expression250
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,956 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,804 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,629 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,136 views
C# Read Json From URL And Parse/Deserialize Json
9,595 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Essay Writers
  • College Essay Writers – The Way to Earn Your Essay in Demand
  • Buy Research Papers – How to See Them at a Bargain Price
  • How to Write a Research Paper For Sale
  • Essay Writing – Some Useful Suggestions for Writing Urgent Essays
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (69)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development