ezrapound Posted June 18, 2012 Share Posted June 18, 2012 I am using php 5.3 with the php 3 and php 4 book. I've hade to substitute preg_match for eregi. It's working for the first two but not the third test, below. Any suggestions for what's wrong? if (!preg_match("#^[[:alpha:].'-]{4,}$#i", stripslashes(trim($_POST['name'])))) { $problem = TRUE; $message .='<p>Please enter a valid name.</p>'; } if (!preg_match("#^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$#i", stripslashes(trim($_POST['email'])))) { $problem = TRUE; $message .='<p>Please enter a valid email address.</p>'; } if (!preg_match("#^((http|https|ftp)://)? ([[:alnum:]-])+(\.) ([[:alnum:]]){2,4} ([[:alnum:]/+=%&_.~?-]*)$ #i", stripslashes(trim($_POST['url'])))) { $problem = TRUE; $message .='<p>Please enter a valid URL.</p>'; } I've tried with several values which should be valid URL names. Link to comment Share on other sites More sharing options...
Larry Posted June 18, 2012 Share Posted June 18, 2012 Just to confirm, are you actually using the first edition of the book? That's a seriously outdated book by this point. In any case, preg_match() and eregi() use two different syntaxes, as you've discovered. When you say the pattern doesn't work, what does that mean? Link to comment Share on other sites More sharing options...
ezrapound Posted June 18, 2012 Author Share Posted June 18, 2012 I submitted items that should evaluate as true. They pass in the first two instances, but the third one fails. Link to comment Share on other sites More sharing options...
HartleySan Posted June 19, 2012 Share Posted June 19, 2012 Just out of curiousity, what purpose does the following part of the regex serve? ([[:alnum:]]){2,4} Are the two sets of brackets required if there are no additional characters within the character class? Also, do you not require the .com, etc. part on the end of the URL? Link to comment Share on other sites More sharing options...
ezrapound Posted June 19, 2012 Author Share Posted June 19, 2012 ([[:alnum:]]){2,4} is to check for the .com, .co, etc. Using the book from which I'w working, the [[:alnum:]] construct seems to be a pre-defined class. Link to comment Share on other sites More sharing options...
Antonio Conte Posted June 19, 2012 Share Posted June 19, 2012 Since you have PHP 5.3, use the Filter extension instead. Defining regexes is tricky business. http://php.net/manua...rs.validate.php As an example, if you would validate an URL, this should work: if ( filter_var($url_to_check, FILTER_VALIDATE_URL) ) { echo 'The url ' . $url_to_check . ' is a valid url'; } else { echo 'Invalid url'; } Link to comment Share on other sites More sharing options...
HartleySan Posted June 19, 2012 Share Posted June 19, 2012 I agree with Antonio. URL's are tricky to parse, and just walking through a couple of example URLs in my head with your regex, I can see why it would fail for legitimate URLs. For example, https://www.google.co.jp would fail, even though it's legit. Why not use the PHP Filter extension as Antonio suggested? Link to comment Share on other sites More sharing options...
Recommended Posts