1934 snippets from 2406 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(160)
stablo
(139)
JavaScript
(138)
Battery
(96)
C
(74)
binarno
(72)
Dell
(68)
Java
(68)
adapter
(65)
polje
(62)
strukture
(60)
VBSCRIPT
(60)
HP
(50)
String
(45)
Batterie
(39)
New Snippets
CompCalc
Nettoyage de tapi...
Nettoyage de mate...
Zaglavlje za stab...
Jewellery trade f...
Industrial trade ...
Batterie asus a42...
Batera Lenovo T...
Batera Lenovo T...
Pokazivac zadatak...
Venture Capital Jobs
New Members
pimteam
zjakupec
Pushkartyagi
niprasnja
lzodan
mlcorak
bonikolic
mezestic1
rekapec
Kasimu
Top Members
dannyboy
sundaramkumar
mattrmiller
all-battery
Pio
Cloudgen
i_kenneth
ASmith
mycodeofshailendra
ctiggerf
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;
}