Jump to content

How to Check URL for proper format?


thewird

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/17606-how-to-check-url-for-proper-format/
Share on other sites

Copied from http://www.weberdev.com/get_example-4227.html

[code]
<?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();
}
?>[/code]
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

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

[code]
<?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";
}

?>

[/code]



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

[code]
<?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";
}

?>
[/code]

//if the format of the url is a www.xxxxxx.com but only a .com
[code]
<?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";
}

?>
[/code]

//if the format of the url is a www.xxxxxx.com but only a .com or .net
[code]
<?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";
}

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.