note 58589 added to function.ucwords

From: Date: Wed, 09 Nov 2005 00:16:48 +0000
Subject: note 58589 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-98281@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_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 :) ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58589 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58589&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58589&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98281) next »