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 Validate URL Use Regular Expression (Regex) In PHP

Validate URL Use Regular Expression (Regex) In PHP

This is just a simple PHP function that verify a input string to see if it’s a valid URL with right format or not. It uses regex expression to match.

This function works for almost cases (website url) but it’s not exhaustive, I’m sure 🙂

PHP Validate URL function

<?php
	function validate_url($url)
	{
		if (!preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$url))
			return "0";
		return "1";
	}
?>

<?php function validate_url($url) { if (!preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$url)) return "0"; return "1"; } ?>

Usage

<?php
	$url = "http://4rapiddev.com";
 
	if(validate_url($url) == "1")
		echo "It's a valid URL";
	else
		echo "It is NOT a valid URL";
?>

<?php $url = "http://4rapiddev.com"; if(validate_url($url) == "1") echo "It's a valid URL"; else echo "It is NOT a valid URL"; ?>

Output

It's a valid URL

It's a valid URL

Jan 31, 2012Hoan Huynh
Disable Auto Save Password In HTML Form Or TextBoxCheck Google PageRank With PHP
You Might Also Like:
  • How To Track Website With Multiple Google Analytisc Accounts
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • PHP validate ip address by using regular expression
  • CSharp Validate Internet URL Function With Regular Expression
  • CSharp Validate Email Address Function With Regular Expression
  • Validate email using regular expression in ASP.NET
  • Get Image Width Height With JQuery And JavaScript
  • Jquery checkbox checked
  • 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 PHPpreg_match, regex expression178
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
24,459 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,656 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,998 views
C# Read Json From URL And Parse/Deserialize Json
11,723 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
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development