Minimize First Letter Of Each Word In A String

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

Minimize First Letter Of Each Word In A String

Post by pedroz »

How can I minimize first letter of each word in a string with a lower version than PHP 5.3?
[edited]

I can do it, but only with lcfirst (PHP 5 >= 5.3.0)

Code: Select all

function lcallfirst($string)
{
$string=explode(" ",$string);
$i=0;
while($i<count($string))
{$string[$i]=lcfirst($string[$i]);
$i++;}
return implode(" ",$string);
} 
Last edited by pedroz on Wed Feb 23, 2011 6:20 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Capitalize First Letter Of Each Word In A String

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: Capitalize First Letter Of Each Word In A String

Post by pedroz »

* wrong question, sorry.
goal is to do the opposite, is putting the first letter in lowercase only!
already edited and changed!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Minimize First Letter Of Each Word In A String

Post by Weirdan »

Code: Select all

if (!function_exists("lcfirst")) {
    function lcfirst($w) {
       if (!is_string($w)) {
           trigger_error(__FUNCTION__ . ' expects string argument', E_USER_NOTICE); 
           return '';
       }
       $len = strlen($w);
       if ($len == 0) return '';
       return strtolower($w[0]) . ($len > 1 ? substr($w, 1) : '');
    }
}
$str = preg_replace("/\b(\w)/e", "lcfirst(\\1)", $str);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Minimize First Letter Of Each Word In A String

Post by s.dot »

Well this is ugly, but it works:

Code: Select all

function lcwords($str)
{
	$ret = array();
	$str = explode(' ', $str);
	
	foreach ($str AS $word)
	{
		$altered = '';
		
		for ($i=0; $i<strlen($word); $i++)
		{
			if (!$i)
			{
				$altered = strtolower($word[$i]);
			} else
			{
				$altered .= $word[$i];
			}
		}
		
		$ret[] = $altered;
	}
	
	return implode(' ', $ret);
}

echo lcwords('I AM A TEST STRING');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Minimize First Letter Of Each Word In A String

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply