Capitals...

For every word with an uneven letter count write the letter in the middle of the word out with a capital by making use of your own functions.

The Sentence is:- PHP is allot of fun for all of us.

How would one do that? Change a the middle letter of a word to a cap??!!!

Hi there,

Not tested this, but works in my head!
[php]$string = ‘PHP is allot of fun for all of us’;
$words = explode(’ ‘,$string);//Split string into an array of words (break the spaces)
foreach($words as $key => $word)
{
$letters = preg_split(’//’,$word,PREG_SPLIT_NO_EMPTY);//Split the word into an array of the letters
if(count($letters)%2 !== 0) //If the number of letters in the word / 2 doesn’t have a remainder of 0
{
$lkey = (count($letters)+1)/2; //Find location of middle letter
$letters[$lkey] = strtoupper($letters[$lkey]); //Replace the middle letter with it’s uppercase equivalent
}
$words[$key] = implode(’’,$letters); //Replace this word in the $words array with the $letters (now with capitalised middle letter)
}
$newstring = implode(’ ',$words); //Create a new string and convert our array of words back into a string (separating the words with a space again)
[/php]

Should work, let me know!

i attempted this same task and the code does not work, can anyone give a tested code? ;D

this is the code i have so far

function capitals($text2)
{

 $_words = explode (" ",$text2);
 $result = " ";
    
    for ($c=0; $c<sizeof($_words); $c++)
       {
          if (strlen(str_replace("."," ",$_words[$c]))%2)
            {
                  $trim = str_replace("."," ",$_words[$c]);
                   $pos = round (count (strlen (str_replace("."," ".$_words[$c]) ) /2) );
                $letter = strtoupper(substr($_words[$c],$pos,1));
                   $new = substr($_words[$c],0,$pos-1).substr_replace($_words[$c],$letter,$pos).substr($_words[$c],$pos+1);
               $result .= $new." ";
            }
         else 
            {
               $result .= $_words[$c]." ";
            }
       }
  return $result;
}

however, it is not capitalizing the middle letter but the one before it on words longer than 3 letters… can anyone help?

Tested my code and altered slightly:

[php]function capmid($string)
{
$words = explode(’ ‘,$string);//Split string into an array of words (break the spaces)
foreach($words as $key => $word)
{
$letters = preg_split(’//’,trim($word));//Split the word into an array of the letters
array_shift($letters);
array_pop($letters);
if(count($letters)%2 !== 0) //If the number of letters in the word / 2 doesn’t have a remainder of 0
{
$lkey = count($letters)/2; //Find location of middle letter
$letters[$lkey] = strtoupper($letters[$lkey]); //Replace the middle letter with it’s uppercase equivalent
}
$words[$key] = implode(’’,$letters); //Replace this word in the $words array with the $letters (now with capitalised middle letter)
}
$newstring = implode(’ ',$words); //Create a new string and convert our array of words back into a string (separating the words with a space again)
return $newstring;
}

echo capmid(‘PHP is allot of fun for all of us’); //result: PHP is alLot of fUn fOr aLl of us [/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service