Page 1 of 1
Minimize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 5:46 am
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);
}
Re: Capitalize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 6:10 am
by s.dot
Re: Capitalize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 6:19 am
by pedroz
* wrong question, sorry.
goal is to do the opposite, is putting the first letter in lowercase only!
already edited and changed!
Re: Minimize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 7:22 am
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);
Re: Minimize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 7:23 am
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');
Re: Minimize First Letter Of Each Word In A String
Posted: Wed Feb 23, 2011 11:38 am
by pickle