thefollower Posted October 25, 2007 Share Posted October 25, 2007 Is there a way to check a variables character count and also to strip out spaces so : cheese bacon ham = cheesebaconham = 14 characters.. then some how assign the 14 to a variable also ? Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/ Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 <?php $var = 'cheese bacon ham'; $count = strlen(str_replace (' ', '', $var)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378240 Share on other sites More sharing options...
thefollower Posted October 25, 2007 Author Share Posted October 25, 2007 thank you.. one other quick question say you wanted to use only numbers and letters... how can you strip away all the symbols on the input ... is there a built in function to do this? Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378249 Share on other sites More sharing options...
DyslexicDog Posted October 25, 2007 Share Posted October 25, 2007 You want to use regex, unfortunately I know nothing regex maybe someone else on the board here can give you a hand. Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378254 Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 you could create an array of illegal characters <?php $remove = array (' ', '#', '?', '$', '*'); $var = 'che$ese ba#con h?a*m'; $count = strlen(str_replace ($remove, '', $var)); echo $count; // 14 ?> Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378255 Share on other sites More sharing options...
thefollower Posted October 25, 2007 Author Share Posted October 25, 2007 it wont let me do all the symbols though, if i do | or + or = for example even when surrounded by ' ' symbols it is acting like its waiting for me to do something with it rather than class it as a char... Only these ones work: $remove = array ('#', '?', '$', '*', '"', ';', ':', '~', '!', '£', '%', '^', '&', '(', ')', ']', '[', '{', '}', '<', '>', ',', '.', '/', '`', '¬', '_'); $count = strlen(str_replace ($remove, '', $NewCode)); Not all of them are there as you can see Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378288 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 Odd <?php $remove = array ('|', '+', '='); $str = 'abc|def+gh=k'; $count = strlen(str_replace($remove, '', $str)); echo $count; // --> 9 ?> Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378295 Share on other sites More sharing options...
kellz Posted October 26, 2007 Share Posted October 26, 2007 here is another way... function Alphanumeric($data) { $NewString=''; preg_match_all("/[a-zA-Z0-9]/", $data, $_); for ($i=0;$i < count($_[0]);$i++) $NewString .= str_replace(" ", "", $_[0][$i]); return $NewString . ' ' . $i; } echo Alphanumeric('test+TEST123=tEsT_'); the result will show only alphanumeric characters and the size of the string will be appended to it, the output of the string used in this example would be: testTEST123tEsT 15 ok everyone already helped but i couldn't resist!^^ pweaase forgive me! Quote Link to comment https://forums.phpfreaks.com/topic/74806-check-string-length/#findComment-378332 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.