upper case

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

upper case

Post 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;
}
User avatar
saltwine
Forum Newbie
Posts: 16
Joined: Tue Nov 09, 2010 7:05 am
Location: London

Re: upper case

Post 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
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: upper case

Post 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;
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: upper case

Post 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.");
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: upper case

Post 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.
Post Reply