Re: eregi help please

From: Date: Tue, 17 Oct 2000 16:12:16 +0000
Subject: Re: eregi help please
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20660@lists.php.net to get a copy of this message
Jeff Lacy [jeffdl@mn.mediaone.net] wrote: > Could some kind soul please help me with eregi? I need something that will return true if > $string doesn't contain anything other than a-z or 0-9 or . or a space. The best thing I have > been able to come up with is > > if (eregi("([a-z]|[0-9]|[.]|[[:space:]])+", $string) { How 'bout if (eregi("^[a-zA-Z\. ]+$", $string)) { I'm more experienced with Perl, so I'm not sure this works, but the concept is - return true if the only characters in the string are letters, periods, and spaces - use the carrot "^" to anchor the regex to the beginning and the dollar sign "$" to anchor the regex to the end - meaning that *ALL* characters in between must match the regex in order to return true. I'm not sure "eregi" uses the ^ and $ as anchors, and at the moment I can't try thiss to confirm it. If you want to return true if you find anything *BUT* those characters, you can use this: if (eregi("[^a-zA-Z\. ]+", $string)) { The carrot at the beginning of the *inside* of the square brackets says "not" - so will return true if it finds any characters which are NOT one of the ones listed. Again, test this, because I'm not sure what PHP's eregi uses in comparison to Perl's regex operators. HTH. -- Hardy Merrill Mission Critical Linux, Inc. http://www.missioncriticallinux.com > echo "$string is okay"; > } > else { > echo "$string is wrong, you fool!!!"; > } > > That doesn't work quite right, though. Could you please help me with it? Thank you so much!

« previous php.general (#20660) next »