Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

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

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

 Share

×
×
  • Create New...