| | |
URL Validation
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2006
Posts: 12
Reputation:
Solved Threads: 0
How do we validate URL either with PHP/javascript? could u help me?for example,
whether input text is in the format http://www.google.com or not?
whether input text is in the format http://www.google.com or not?
•
•
Join Date: Aug 2006
Posts: 138
Reputation:
Solved Threads: 2
php Syntax (Toggle Plain Text)
if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) { echo "wrong url"; } else { echo "ok"; }
•
•
•
•
php Syntax (Toggle Plain Text)
if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) { echo "wrong url"; } else { echo "ok"; }
any website with a subdomain/or lack of one other than www, for instance
http://slashdot.org/ (has no www and no subdomain)
http://en.wikipedia.org/ (has a subdomain en, but no www)
subfolders
http://www.mozilla.com/en-US/firefox/
ip addresses...
http://64.233.167.99/
you will actually get google.com (that is its ip address as of this current moment).
specifying port numbers:
http://glcfapp.umiacs.umd.edu:8080
entering username/passwords directly into the url
http://username: password@example.com
i recommend going here: http://regexlib.com/Search.aspx
and performing a search for category "Uri" and see which regular expression you thing would work best for this example. here is a sample i got from that site, of course this one is not perfect either, but it's another sample.
PHP Syntax (Toggle Plain Text)
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
just know that it will probably be virtually impossible to find a perfect regex expression to match all possible URL's, one way would be to see if it starts with http:// and if it does, then see if you can make a connection to it with php, if the headers return a status code of 200, then you are good and you know it's a valid url, but that is a lot of work in itself.
Last edited by paradox814; Dec 29th, 2006 at 1:00 am.
•
•
Join Date: Jun 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#5 21 Days Ago
check this out
PHP Syntax (Toggle Plain Text)
<?php $subscr=$_REQUEST['subscr']; //url validation if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $subscr)) { print "$subscr url OK."; } else { print "$subscr url not valid!"; } //echo("$subscr"); ?>
Last edited by ym_chaitu; 21 Days Ago at 7:51 am. Reason: previous code is getting failed
![]() |
Similar Threads
- XHTML with PHP validation ??? (HTML and CSS)
- URL Validation (JSP)
Other Threads in the PHP Forum
- Previous Thread: how to draw tree using gd
- Next Thread: How to insert csv file data to mysql table selected colum
| Thread Tools | Search this Thread |
.htaccess ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl customizableitems database date delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube





