note 91145 deleted from function.preg-split by danbrown

From: Date: Wed, 27 May 2009 13:47:59 +0000
Subject: note 91145 deleted from function.preg-split by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156061@lists.php.net to get a copy of this message
Note Submitter: chris AT cmbuckley DOT co DOT uk ---- The previous function failed to take into account the possibility that the first letter was lower-case. Here's a fix (also adding $glue parameter): <?php function spacify($camel, $glue = ' ') { return $camel[0] . substr(implode($glue, array_map('implode', array_chunk(preg_split('/([A-Z])/', ucfirst($camel), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE), 2))), 1); } echo spacify('CamelCaseWords'); // 'Camel Case Words' echo spacify('camelCaseWords'); // 'camel Case Words' ?>

« previous php.notes (#156061) next »