Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.