dsaba Posted November 30, 2007 Share Posted November 30, 2007 <?php function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't $flag = true; $x = -1; do { $x = $x + 1; $char = substr($str,$x,1); if (!in_array(strtolower($char),$charArr)) { $flag = false; echo $char; } } while($flag || $x < strlen($str)); return $flag; } $str = '133444444444444444444444444444444444'; if (checkStr($str,array('1','2','3','4'))) { echo 'yes'; } else { echo 'no'; } ?> Why doesn't it echo 'yes', I think it should -thanks Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 1, 2007 Author Share Posted December 1, 2007 fixed changed "||" to "&&" solved Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted December 1, 2007 Share Posted December 1, 2007 <?php function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't $flag = true; $x = 0; do { if (!in_array(strtolower($str{$x}),$charArr)) { $flag = false; echo $str{$x}; break; } $x++; } while($x < strlen($str)); return $flag; } $str = '133444444444444444444444444444444444'; if (checkStr($str,array('1','2','3','4'))) { echo 'yes'; } else { echo 'no'; } ?> a slight;ly more elegant soluntion IMO... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.