By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

preg_match() regex to validate URL

P: n/a
As I understand it, the characters that make up an Internet domain name can
consist of only alpha-numeric characters and a hyphen
(http://tools.ietf.org/html/rfc3696)

So I'm trying to write regex that will provide a basic url format validation:

starts with http or https (the only 2 prots I'm interested in), is followed by
'://', then ([any alpha-numeric or hyphen] followed by a '.' appearing 1 or more
times), then followed by anything *, and is case-insensitive.

I tried this:

if (preg_match('/^(http|https):\/\/([a-z0-9-]\.+)*/i', $urlString))
{
$valid == true;
}
else
{
$valid == false;
}

but no luck.

Any suggestions welcome...

Thanks in advance.

Feb 12 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Rik
deko wrote:

Deko, while you enthusiasm is appreciated, please stay in the same thread
when making a post about the same subject. Starting several threads not
only creates confusion about answers already given and context, it also
gives off the feeling of being very pushy.
As I understand it, the characters that make up an Internet domain name
can consist of only alpha-numeric characters and a hyphen
(http://tools.ietf.org/html/rfc3696)
...."Any characters, or combination of bits (as octets), are permitted in
DNS names. However, there is a preferred form that is required by most
applications.".....
So I'm trying to write regex that will provide a basic url format
validation:

starts with http or https (the only 2 prots I'm interested in), is
followed by '://', then ([any alpha-numeric or hyphen] followed by a '.'
appearing 1 or more times), then followed by anything *, and is
case-insensitive.

I tried this:

if (preg_match('/^(http|https):\/\/([a-z0-9-]\.+)*/i', $urlString))
This bit "([a-z0-9-]\.+)" does not do what you think it does, it matches
_one_ single character in the [a-z0-9-]-range, followed by at least one,
but an arbitrary amount of literal dots. And that repeated zero or more
times.. So 'http://a.b.c.d......d..e....a......' would match.

Further more, you seem to have anchorder this with ^, so it will only
match if http(s):// is at the very beginning of the string. Is that whatr
you want?

'/^https?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)+/i'

--
Rik Wasmus
Feb 12 '07 #2

P: n/a
>As I understand it, the characters that make up an Internet domain name can
>consist of only alpha-numeric characters and a hyphen
(http://tools.ietf.org/html/rfc3696)
..."Any characters, or combination of bits (as octets), are permitted in DNS
names. However, there is a preferred form that is required by most
applications.".....
I just tried registering various domain names with an underscore. The
registrar's system rejected it. While this may not be the best verification, I
have yet to see a valid Internet domain with an underscore or any other
non-alphanumeric character (other than a hyphen).
'/^https?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)+/i'
Thanks, Rik

Feb 12 '07 #3

P: n/a
Rik
On Mon, 12 Feb 2007 10:29:26 +0100, deko <de**@nospam.comwrote:
>>As I understand it, the characters that make up an Internet domain
name can consist of only alpha-numeric characters and a hyphen
(http://tools.ietf.org/html/rfc3696)
..."Any characters, or combination of bits (as octets), are permitted
in DNS names. However, there is a preferred form that is required by
most applications.".....

I just tried registering various domain names with an underscore. The
registrar's system rejected it. While this may not be the best
verification, I have yet to see a valid Internet domain with an
underscore or any other non-alphanumeric character (other than a hyphen).
There are efforts to fully internationalise DNS entries, so even non-roman
based character sets are allowed. See for instance
<http://www.ietf.org/rfc/rfc4185.txt>. We're not there yet by a long shot,
but there's no doubt it will happen.

--
Rik Wasmus
Feb 12 '07 #4

P: n/a
>>>As I understand it, the characters that make up an Internet domain name
>>>can consist of only alpha-numeric characters and a hyphen
(http://tools.ietf.org/html/rfc3696)
..."Any characters, or combination of bits (as octets), are permitted in
DNS names. However, there is a preferred form that is required by most
applications.".....

I just tried registering various domain names with an underscore. The
registrar's system rejected it. While this may not be the best
verification, I have yet to see a valid Internet domain with an underscore
or any other non-alphanumeric character (other than a hyphen).

There are efforts to fully internationalise DNS entries, so even non-roman
based character sets are allowed. See for instance
<http://www.ietf.org/rfc/rfc4185.txt>. We're not there yet by a long shot,
but there's no doubt it will happen.
Eventually, I'm sure.

Getting back to my regex question, I wonder if it would be better to check for
illegal characters:

if
(preg_match('/(`|~|!|@|#|$|%|^|&|*|(|\)|_|\+|=|\[|\{|\]|\}|\||;|\:|\'|\"|\<|\>|\?|)/',
$url_a['host'])) ???

I'm not having much luck catching invalid hostnames otherwise...

Feb 12 '07 #5

P: n/a
Rik wrote:
There are efforts to fully internationalise DNS entries, so even non-roman
based character sets are allowed. See for instance
<http://www.ietf.org/rfc/rfc4185.txt>. We're not there yet by a long shot,
but there's no doubt it will happen.
Not there yet?!

Try telling that to "www.한글.kr"!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 12 '07 #6

P: n/a
..oO(Rik)
>'/^https?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)+/i'
With another delimiter you could avoid the escaping of slashes and make
the regexp a bit more readable (IMHO):

'#^https?://[a-z0-9-]+(\.[a-z0-9-]+)+#i'

Just my 2 cents.

Micha
Feb 12 '07 #7

P: n/a
Rik
On Mon, 12 Feb 2007 16:39:24 +0100, Toby A Inkster
<us**********@tobyinkster.co.ukwrote:
Rik wrote:
>There are efforts to fully internationalise DNS entries, so even
non-roman
based character sets are allowed. See for instance
<http://www.ietf.org/rfc/rfc4185.txt>. We're not there yet by a long
shot,
but there's no doubt it will happen.

Not there yet?!

Try telling that to "www.한글.kr"!
Yup, works. Isn't understood by a lot of programs though, most browsers
will handle it just fine, but browsing is not the only thing we want to
use it for.

Simple example: I cannot ping this with ease in my Windows version...
--
Rik Wasmus
Feb 12 '07 #8

P: n/a
>>'/^https?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)+/i'
>
With another delimiter you could avoid the escaping of slashes and make
the regexp a bit more readable (IMHO):

'#^https?://[a-z0-9-]+(\.[a-z0-9-]+)+#i'
Thanks for the tip.

I recently found this: http://baseclass.modulweb.dk/urlvali...viewsource.php

which looks interesting, if not overkill.
Feb 13 '07 #9

P: n/a
Rik wrote:
Yup, works. Isn't understood by a lot of programs though, most browsers
will handle it just fine, but browsing is not the only thing we want to
use it for.

Simple example: I cannot ping this with ease in my Windows version...
There are IDN-enabled versions of ping available, but few if any operating
systems ship with them as standard yet.

Though the hope is that operating systems will integrate IDN support
directly into their own gethostbyname() type functions, so there is no
need to explicitly compile IDN support into all software that uses domain
names.

(On the other hand, many software has to parse URLs too, in which case
they'd probably need to update their URL-parsing code to cope with IDN.)

libidn exists, which makes it really easy to drop in support for
internationalised domain names into existing network apps. It's LGPL too,
which even makes it available for use by closed-source software.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.