775 snippets from 1546 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(136)
JavaScript
(123)
Java
(66)
VBSCRIPT
(58)
String
(44)
CSS
(31)
CSharp
(28)
File
(28)
HTML
(27)
C
(24)
mysql
(24)
VB.NET
(24)
python
(24)
CPlusPlus
(23)
groovy
(23)
New Snippets
Top 15 Ways To Ma...
Top 15 Ways To Ma...
Top 15 Ways To Ma...
Top 15 Ways To Ma...
Top 15 Ways To Ma...
Very lightweight ...
AutoComplete plug...
AutoComplete plug...
Connection Java -...
View PostgreSql
Venture Capital Jobs
New Members
NationalCollection
me
jamesmcm
Can
Kelmi
ysg
dannymo2
chorny
wallie
Hackdemian
Top Members
dannyboy
sundaramkumar
mattrmiller
Pio
i_kenneth
ASmith
ctiggerf
sehrgut
bertheymans
SCoon
Home
/
Snippets
/
Regex functino to validate a URL
/
Comments
Regex functino to validate a URL
Snippet Menu
Revisions
Comments
Related Snippets
Add to Favorites
Email Snippet
Download Snippet
Print Snippet
Blog Snippet
snippet
|
revisions
|
comments
|
related
|
Add to Favorites
|
email
download
|
print
|
blog it
New Comment
Works for most URLs
Mon. Oct. 2nd, 2006 9:29 AM
ctiggerf
Your solution works for most URLs but not all.
One example that would not work with yours is ...
http://www.google.de/
or any other URL from a country TLD.
Here is a more inclusive test:
function
isValidUrl
(
$Url
)
{
//check for an empty url first
if
(
empty
(
$Url
)
)
{
return
false
;
}
//let php tell us if it as a valid url
$UrlElements
=
parse_url
(
$Url
)
;
if
(
(
empty
(
$UrlElements
)
)
or
(
!
$UrlElements
)
)
{
return
false
;
}
$scheme
=
$UrlElements
[
scheme
]
;
$HostName
=
$UrlElements
[
host
]
;
//check to make sure it has a scheme and hostname
if
(
empty
(
$scheme
)
or
empty
(
$HostName
)
)
{
return
false
;
}
//check the scheme (removes mailto and others)
if
(
!
eregi
(
"^(ht|f)tp"
,
$scheme
)
)
{
return
false
;
}
return
true
;
}
Reply
New Comment
One example that would not work with yours is ...
http://www.google.de/
or any other URL from a country TLD.
Here is a more inclusive test:
function isValidUrl($Url) {
//check for an empty url first
if (empty($Url)) { return false; }
//let php tell us if it as a valid url
$UrlElements = parse_url($Url);
if( (empty($UrlElements)) or (!$UrlElements) ) { return false; }
$scheme = $UrlElements[scheme];
$HostName = $UrlElements[host];
//check to make sure it has a scheme and hostname
if(empty($scheme) or empty($HostName)) { return false; }
//check the scheme (removes mailto and others)
if (!eregi("^(ht|f)tp",$scheme)) { return false; }
return true;
}