note 94465 added to function.strtoupper

From: Date: Fri, 06 Nov 2009 16:38:49 +0000
Subject: note 94465 added to function.strtoupper
Groups: php.notes 
Request: Send a blank email to php-notes+get-162459@lists.php.net to get a copy of this message
I liked jesdisciple's approach to a camel case function, but needed something a little more high powered, so here is my stab at this need: <?php /** * Convert a string to camel case, optionally capitalizing the first char and optionally setting which characters are * acceptable. * * First, take existing camel case and add a space between each word so that it is in Title Form; note that * consecutive capitals (acronyms) are considered a single word. * Second, capture all contigious words, capitalize the first letter and then convert the rest into lower case. * Third, strip out all the non-desirable characters (i.e, non numerics). * * EXAMPLES: * $str = 'Please_RSVP: b4 you-all arrive!'; * * To convert a string to camel case: * strtocamel($str); // gives: PleaseRsvpB4YouAllArrive * * To convert a string to an acronym: * strtocamel($str, true, 'A-Z'); // gives: PRBYAA * * To convert a string to first-lower camel case without numerics but with underscores: * strtocamel($str, false, 'A-Za-z_'); // gives: please_RsvpBYouAllArrive * * @param string $str text to convert to camel case. * @param bool $capitalizeFirst optional. whether to capitalize the first chare (e.g. "camelCase" vs. "CamelCase"). * @param string $allowed optional. regex of the chars to allow in the final string * * @return string camel cased result * * @author Sean P. O. MacCath-Moran www.emanaton.com */ function strtocamel($str, $capitalizeFirst = true, $allowed = 'A-Za-z0-9') { return preg_replace( array( '/([A-Z][a-z])/e', // all occurances of caps followed by lowers '/([a-zA-Z])([a-zA-Z]*)/e', // all occurances of words w/ first char captured separately '/[^'.$allowed.']+/e', // all non allowed chars (non alpha numerics, by default) '/^([a-zA-Z])/e' // first alpha char ), array( '" ".$1', // add spaces 'strtoupper("$1").strtolower("$2")', // capitalize first, lower the rest '', // delete undesired chars 'strto'.($capitalizeFirst ? 'upper' : 'lower').'("$1")' // force first char to upper or lower ), $str ); } ---- Server IP: 69.147.83.197 Probable Submitter: 8.2.183.171 ---- Manual Page -- http://www.php.net/manual/en/function.strtoupper.php Edit -- https://master.php.net/note/edit/94465 Del: integrated -- https://master.php.net/note/delete/94465/integrated Del: useless -- https://master.php.net/note/delete/94465/useless Del: bad code -- https://master.php.net/note/delete/94465/bad+code Del: spam -- https://master.php.net/note/delete/94465/spam Del: non-english -- https://master.php.net/note/delete/94465/non-english Del: in docs -- https://master.php.net/note/delete/94465/in+docs Del: other reasons-- https://master.php.net/note/delete/94465 Reject -- https://master.php.net/note/reject/94465 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162459) next »