Jump to content


Photo

How to Check URL for proper format?


  • Please log in to reply
7 replies to this topic

#1 thewird

thewird
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 15 August 2006 - 10:34 AM

I was wondering how you check a URL in a variable to see that it is in proper format. What I mean by this is that it has no illegal characters for URL and can exist but doesn't mean it necessarily exists (don't need to check if it actually exists on the web). If anyone could help me with this, that would be great. Thanks.

thewird

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 August 2006 - 11:23 AM

Copied from http://www.weberdev....ample-4227.html

<?php
/*
The next code checks the following :

o URL starts with a-z, A-Z (e.g. http, ftp...)
o It is then followed by ://
o next we have more letters, numbers and special chars : a-z, A-Z, 0-9, -, _
o there must be at least one "."
o A URL can contain at it's end part the more special characters and this is
why the last part allows for more options such as ?, /, &, %, etc...
you may add/remove chars as needed to your code.
*/

// Assuming that the URL is received from a FORM by POST with the field name being "URL" the code
// Looks like this :

if(!preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i",$_POST[URL])) {
Echo"You must supply a valid URL.";
Exit();
}
?>

Legend has it that reading the manual never killed anyone.
My site

#3 thewird

thewird
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 15 August 2006 - 12:13 PM

Thank you very much. It works great :).

thewird

#4 thewird

thewird
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 15 August 2006 - 12:34 PM

Oh I just realized it allows for any urls. What I was really looking for and  wasn't quite clear was for domain name only, meaning something.extension (ie domain.com)

For the preg_match part i did...

preg_match("/^[A-Za-z0-9\-_]+\\.+[A-Za-z0-9-_]+$/i",$domainname)

However, I'm not sure what +$/i does. Could anyone help? THanks.

thewird

#5 thewird

thewird
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 15 August 2006 - 01:11 PM

Anyone?

thewird

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 August 2006 - 02:21 PM


Your getting a head start hear but relly you need to read the manual on regular exspestions


good luck


//if the format of the url is a www.xxxxxx.com

<?php

$url="www.phpfreaks.com";

if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.[a-zA-Z]{3}",$url)){

echo "incorrect web address";

}else{

echo "correct web address";
}

?>




//if the format of the url is a http://www.xxxxxx.com

<?php

$url="http://www.phpfreaks.com";

if(!eregi("^(http://)[a-zA_Z]{3}.[a-zA-Z]{1,100}\.[a-zA-Z]{3}",$url)){

echo "incorrect web address";

}else{

echo "correct web address";
}

?>

//if the format of the url is a www.xxxxxx.com but only a .com
<?php

$url="www.phpfreaks.com";

if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.(com)",$url)){

echo "incorrect web address";

}else{

echo "correct web address";
}

?>

//if the format of the url is a www.xxxxxx.com but only a .com or .net
<?php

$url="www.phpfreaks.net";

if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.(com|net)",$url)){

echo "incorrect web address";

}else{

echo "correct web address";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 15 August 2006 - 04:45 PM

$/i is for case-insensative I believe.

#8 thewird

thewird
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 15 August 2006 - 05:43 PM

$/i is for case-insensative I believe.


Ok thanks. I think the code I posted above should do the trick then.

thewird




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users