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
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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites


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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.