Page 1 of 1

upper case

Posted: Tue Nov 09, 2010 4:22 am
by pedroz
Any idea to change the code below in order to have this?
(single word)
abc => Abc ... working

(one or two chars in the middle of the words, small case)
abcd xe efg hij => Abcd xe Efg Hij ... not working
abcd x efg => Abcd x Efg ... not working


(one or two chars in beginning or end, upper case)
ab cde => Ab Cde ... working
abc de => Abc De ... working
a bc => A Bc ... working

(if it has a point)
ab.cd => Ab.cd

(other char)
abc - de => Abc - De

Code: Select all

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}

Re: upper case

Posted: Tue Nov 09, 2010 8:22 am
by saltwine
Hi pedroz

It's a little unclear what you're trying to do with your attached code.

From your working/not working examples the native ucwords() function does this (see http://php.net/manual/en/function.ucwords.php).

Code: Select all

echo ucwords('ab cde');	// Ab Cde
echo ucwords('abc de');	// Abc De
echo ucwords('a bc');	// A Bc

echo ucwords('ab.cd');	// Ab.cd

echo ucwords('abc - de');	// Abc - De

Re: upper case

Posted: Wed Nov 10, 2010 6:58 am
by pedroz
Sorry about the explanation in the first post not very clear.

I need a function which converts uppercase the first letter of the word, except when its length is less than 3 chars.

Example:
echo function('abcd xe efg hij'); //Abcd xe Efg Hij
echo function('aBcd X efg'); //Abcd x Efg

May be trying to improve this will solve. adding something to the delimiter array

Code: Select all

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}

Re: upper case

Posted: Wed Nov 10, 2010 11:32 am
by Celauran
You don't mean something like this, do you?

Code: Select all

function ucname($string)
{
    $string = strtolower($string);
    $words = explode(" ", $string);
    $result = '';
    
    foreach ($words as $word)
    {
        if (strlen($word) > 2)
        {
            $word = ucwords($word);
        }
        $result .= $word . " ";
    }
    
    return rtrim($result);
}

echo ucname("abcd ef ghi, jk lmnop q rstuv.");

Re: upper case

Posted: Wed Nov 10, 2010 12:18 pm
by s992
I'm going to make a slight modification to Celauran's function because it looks like you want to make anything less than two character lowercase.

Code: Select all

<?php
function ucname($string)
{
    $string = strtolower($string);
    $words = explode(" ", $string);
    $result = '';
   
    foreach ($words as $word)
    {
		$word = (strlen($word) > 2) ? ucwords($word) : strtolower($word);
		$result .= $word . " ";
    }
   
    return rtrim($result);
}

echo ucname("abcd Ef ghi, jK lmnop Q rstuv."); //Abcd ef Ghi, jk Lmnop q Rstuv.