Re: Still REG EX

From: Date: Mon, 25 Mar 2002 09:19:57 +0000
Subject: Re: Still REG EX
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-90002@lists.php.net to get a copy of this message
Have a look at this: function ValidateUrl($url) { // Check the syntax if (!preg_match("!((https?|ftp)://)(www\.)?([a-zA-Z0-9/\-]*)+\.([a-zA-Z0-9/\-]* )+([a-zA-Z0-9/\-\.]*)?!", $url)) { return false; } // If it's correct, then try and open it.... if (!@fopen($url, "r")) { return false; } return true; } if (!ValidateUrl("http://www.yoursite.com")) { echo "Invalid url"; } else { echo "It's there."; } Not particularly clean, but it seems to work ok. Will check for http, https, ftp, followed by an optional www. followed by numbers, digits, hyphens etc. If it gets passed that check, then the function then attempts to open the url. You may need to play around with it a little... ~James "John Fishworld" <john@webital.de> wrote in message news:004001c1d2cc$caff60e0$04010a0a@fishworld... > Okay good point but still why doesn't it work ? > > thanks > john > > > > On Sun, 24 Mar 2002, John Fishworld wrote: > > > I'm still playing about trying to validate an url > > > www(dot)something(dot)something ! > > > > > > I thought this would work but know > > > > > > if (ereg("^(w{3})(\\.)([a-zA-Z]+)(\\.)([a-z]{2,4})$", $str)) > > > > First of all, web server hostnames don't need to start with www. Secondly, > > they can have any number of components (separated by periods) from 1 to > > dozens. Thirdly, the last term doesn't have to be 4 characters or less. > > > > > ([a-z]{2,4})$ = at least 2 leters eg de but upto 4 eg info > > > > .museum... > > > > miguel > > > > > >

« previous php.general (#90002) next »