note 58589 deleted from function.ucwords by cmb

From: Date: Sun, 04 Aug 2019 08:44:48 +0000
Subject: note 58589 deleted from function.ucwords by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211787@lists.php.net to get a copy of this message
Note Submitter: gothicbunny at hotmail dot com ---- 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 :)

« previous php.notes (#211787) next »