note 58589 modified in function.ucwords by cece
| From: | cece@php.net | Date: | Wed, 09 Nov 2005 13:37:39 +0000 |
| Subject: | note 58589 modified in function.ucwords by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-98327@lists.php.net to get a copy of this message | ||
Here is a simple, yet winded, opposite to ucwords.
<?php
/*
# lcwords v1.000
# Convert the first word character to lowercase (opposite to ucwords)
# input string
# return string
*/
function lcwords($string)
{
/* Some temporary variables */
#loop variable
$a = 0;
#store all words in this array to be imploded and returned
$string_new = array();
#create array of all words
$string_exp = explode(" ",$string);
foreach($string_exp as $astring)
{
for($a=0;$a<strlen($astring);$a++)
{
#check that the character we are at {pos $a} is a word
#i.e. if the word was !A the code would fail at !
#then loop to the next character and succeed at A
#check at character position $a
if(preg_match("'\w'",$astring[$a]))
{
$astring[$a] = strtolower($astring[$a]);
#end the loop
break;
}
}
$string_new[] = $astring;
}
#recreate the string from array components using space deliminator
return implode(" ",$string_new);
}
?>
Of course a simplier way would be to use a callback, but I like working with long code :)
--was--
Here is a simple, yet winded, opposite to ucwords.
<?php
/*
# lcwords v1.000
# Convert the first word character to lowercase (opposite to ucwords)
# input string
# return string
*/
function lcwords($string)
{
/* Some temporary variables */
#loop variable
$a = 0;
#store all words in this array to be imploded and returned
$string_storage = array();
#create array of all words
$string_exp = explode(" ",$string);
foreach($string_exp as $astring)
{
for($a=0;$a<strlen($astring);$a++)
{
#check that the character we are at {pos $a} is a word
#i.e. if the word was !A the code would fail at !
#then loop to the next character and succeed at A
#check at character position $a
if(preg_match("'\w'",$astring[$a]))
{
$astring[$a] = strtolower($astring[$a]);
#end the loop
break;
}
}
$string_new[] = $astring;
}
#recreate the string from array components using space deliminator
return implode(" ",$string_new);
}
?>
Of course a simplier way would be to use a callback, but I like working with long code :)
http://php.net/manual/en/function.ucwords.php