Inconsistent behavior with preg_match_all in conditional
| From: | Randy Janinda | Date: | Wed, 29 May 2002 03:06:18 +0000 |
| Subject: | Inconsistent behavior with preg_match_all in conditional | ||
| Groups: | php.dev php.general | ||
| Request: | Send a blank email to php-dev+get-84516@lists.php.net to get a copy of this message | ||
Hello everyone,
I have a script to check a user submitted string. It may be multiple
words. If a word is found to be 3 characters or less, the script should
retain the case of the string IF all the characters are uppercase.
Otherwise, the script will make the first character of each word
uppercase. So "Main st" becomes "Main St" and "Main st NB" becomes
"Main
St NB". Easy enough right?
So take a look and let me know if this is a bug or feature.
Here's the chunk of code and you can test it in action at
http://www.tqlabs.com/testcase.php (and .phps
accordingly):
if ($MyString) {
$safeInfo = explode(" ",strip_tags(stripslashes(trim($MyString))));
foreach($safeInfo as $strTok) {
print "Checking: $strTok<BR>";
if (strlen($strTok) <= 3) {
$case = preg_match_all("/[A-Z]/",$strTok,$junk);
$count = strlen($strTok);
///////////////////////////////////////
// Why won't this work?!?!?!
///////////////////////////////////////
if (preg_match_all("[A-Z]",$strTok,$junk) == strlen($strTok)) {
/////////////////////////
// But this will ?????
/////////////////////////
//if ($case == $count) {
// They are all uppercase and will remain so
echo "Keep em<BR>";
$finalStr .= "$strTok ";
}else{
// Case seems random....
$finalStr .= ucwords(strtolower($strTok))." ";
}
}else{
$finalStr .= ucwords(strtolower($strTok))." ";
}
}
echo "<B>Final: $finalStr</B><BR>";
}