amrclutch1 Posted September 21, 2007 Share Posted September 21, 2007 I need to count the number of characters from a word in a string with spaces. EX: Let the string be "user_cp". perform a str_replace("_", " ", ucwords(trim(strtolower($string)))) Therefore I have "User cp". Now I want to perform something that counts the number of words in it, excluding spaces, then checks if it has only 2 characters, if so, perform a strtoupper($selectedword); Then extracting it back to the string as "User CP". I am looking forward to any help! Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/ Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 try using strpos to search Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351937 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 <?php $user = 'Selected cp'; $pos = strpos($user, 'c','p'); /// will return first occurence of 'cp' $replace = strupper($pos); /// replaces first occurence of cp with CP ?> Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351940 Share on other sites More sharing options...
hvle Posted September 21, 2007 Share Posted September 21, 2007 There is a PHP built in function "str_word_count" Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351946 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 my code does the same thing but for the first occurence of it only Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351947 Share on other sites More sharing options...
amrclutch1 Posted September 21, 2007 Author Share Posted September 21, 2007 I can't use strpos, I need it to search for any words with 2 chars, not necessarily cp only, any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351948 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 you should use eregi replace only other thing i can think of Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-351952 Share on other sites More sharing options...
Fadion Posted September 21, 2007 Share Posted September 21, 2007 Im certainly sure there's a simpler and faster method but this is what i came with: <?php $sentence = 'This is some text on a variable'; $words = explode(' ', $sentence); for($i=0; $i<count($words); $i++){ if(strlen($words[$i]) == 2){ $words[$i] = strtoupper($words[$i]); } } $sentence = implode(' ', $words); echo $sentence; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-352069 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.