You are here: Re: preg_match() regex to validate URL « PHP Programming Language « IT news, forums, messages
Re: preg_match() regex to validate URL

Posted by Rik on 02/12/07 09:15

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация