Jump to content

Help with strings


amrclutch1

Recommended Posts

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! :D

Link to comment
https://forums.phpfreaks.com/topic/70076-help-with-strings/
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/70076-help-with-strings/#findComment-352069
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.