gigantorTRON Posted October 1, 2007 Share Posted October 1, 2007 Hey all... I need a function that will return the number of characters in a string. I've tried several including count_chars, but it's not working for what I need because the value is coming in as '18a' for example and count_chars will then return 3. Is there another function/combination of functions that I can use to count only the 'a' in a string?? I've looked through the manual but couldn't find an appropriate function. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/71302-solved-return-number-of-characters-in-a-string/ Share on other sites More sharing options...
sKunKbad Posted October 1, 2007 Share Posted October 1, 2007 strlen Quote Link to comment https://forums.phpfreaks.com/topic/71302-solved-return-number-of-characters-in-a-string/#findComment-358732 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 <?php $string = "adfsgfa23aa13aa21a"; $letters = str_split($string); $count = 0; foreach ($letters as $letter){ if ($letter == 'a') $count++; } echo "There were <b>$count</b> a's in the string"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71302-solved-return-number-of-characters-in-a-string/#findComment-358734 Share on other sites More sharing options...
gigantorTRON Posted October 1, 2007 Author Share Posted October 1, 2007 Thanks for the replies.. strlen won't work as the value being checked is a number followed by a character... '18a' which comes out to 3. Looks like str_split will work... just need to set the if checking to a range [a-z]. Quote Link to comment https://forums.phpfreaks.com/topic/71302-solved-return-number-of-characters-in-a-string/#findComment-358736 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.