Valid URLs

Status
Not open for further replies.

Chris2k

Banned
Banned
901
2009
17
0
Hi, I wanna adda valid url's c heck to my remote uploader.

I have an idea of how to do it, like

$url = "http://";

if ($url) {

//do this
}

but the end user cud add any url atm like warezscene.net/mypic.png or krazywarez.com/mypic.png or xtremeddl.net/mypic.png so i need to find some code for differnt url's.

any1 help?
 
7 comments
You could use regex to validate urls

This function from: http://www.blog.highub.com/regular-...egular-expression/php-regex-validating-a-url/

PHP:
function validateURL($url){$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';return preg_match($pattern, $url);}
And can check if the string starts with www, then add http or if having no www/http then add http
 
This seems a good yet clean function, however when i input a link i want a windows error msg to pop up if invalid url...

is it possible and do u no how?
 
If in php, you can use this:

PHP:
function addnvalidateurls($url){
// Modified By: Soft2050
if (!preg_match("@^[hf]tt?ps?://@", $url)) {
        $url = "http://" . $url;
}
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match($pattern, $url);
}

The above code will check for all things

It will also add http if not in url and then check whether url is valid or not

However for displaying a error, you need to use javascript with it
 
Check this code:

PHP:
<?php
function addnvalidateurls($url){
// Modified By: Soft2050
if (!preg_match("@^[hf]tt?ps?://@", $url)) {
        $url = "http://" . $url;
}
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match($pattern, $url);
} 

$result = addnvalidateurls('http://www.google.com');
echo '<script language="javascript">';
if ($result == 1)
echo "alert('Yes! The given url is validated')";
else {
echo "alert('No! the given url seems to be not working')";
}
echo '</script>';
?>

You can add this code before remote uploading the url

If Url is not valid, then you can break the function to end it :)
 
we seem to have a problem, i tried uploading: C:\wamp\www\includes

and it said its valid lol when its not also i read ccomments on that blog saying it ddidnt work.
 
Try this now:

PHP:
<?php
function addnvalidateurls($url){
// Modified By: Soft2050
if (!preg_match("@^[hf]tt?ps?://@", $url)) {
        $url = "http://" . $url;
}
$pattern = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';
return preg_match($pattern, $url);
} 

$result = addnvalidateurls('google.com');
echo '<script language="javascript">';
if ($result == 1)
echo "alert('Yes! The given url is validated')";
else {
echo "alert('No! the given url seems to be not working')";
}
echo '</script>';
?>
Regex Code: http://snipplr.com/view/36992/improvement-of-url-interpretation-with-regex/

Or have a search for some premade regex using google or you can code yourself if you are good at it
 
Status
Not open for further replies.
Back
Top