TEENFRONT Posted February 16, 2009 Share Posted February 16, 2009 Hey I have a chat system and i need to check if a string contains only 1 character for example $var = "......................."; i need to block out that, so id need to check if $var only contained a full stop, or anything like $var = "aaaaaaaaaaaaaaaaaaaa"; etc. Im trying to stop people from flooding the chat with fullstops and one letters etc. i can stop them flooding when $var is empty by checking if $var = ""; but i dont know how to check if the var only contains one set of characters repeated like the above examples, any help? Quote Link to comment https://forums.phpfreaks.com/topic/145355-check-if-a-string-has-different-characters/ Share on other sites More sharing options...
jackpf Posted February 16, 2009 Share Posted February 16, 2009 Hey I have a chat system and i need to check if a string contains only 1 character for example $var = "......................."; i need to block out that, so id need to check if $var only contained a full stop, or anything like $var = "aaaaaaaaaaaaaaaaaaaa"; etc. Im trying to stop people from flooding the chat with fullstops and one letters etc. i can stop them flooding when $var is empty by checking if $var = ""; but i dont know how to check if the var only contains one set of characters repeated like the above examples, any help? <?php $str = $_POST['form']; $num_char = strlen($str); $num_char = $num_char - 1; for($counter =0; $counter <= $num_char; $counter++) { if($str[$counter] == $first || $str[$counter] == $second) { $error = 1; break;} $first = $str[$counter -2 ]; $second =$str[$counter -1 ]; } if($error) { echo 'Error, you are entering repeat characters.'; } ?> Hope this helps, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/145355-check-if-a-string-has-different-characters/#findComment-763068 Share on other sites More sharing options...
TEENFRONT Posted February 16, 2009 Author Share Posted February 16, 2009 il go test this now, very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/145355-check-if-a-string-has-different-characters/#findComment-763076 Share on other sites More sharing options...
jackpf Posted February 16, 2009 Share Posted February 16, 2009 No problem, hope it works. Quote Link to comment https://forums.phpfreaks.com/topic/145355-check-if-a-string-has-different-characters/#findComment-763083 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.