tag:blogger.com,1999:blog-6950106.post7112035709902867604..comments2008-07-12T00:46:20.969ZComments on Charl van Niekerk ยป Blog: PHP CamelCase Explode 2.0Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929charlvn@charlvn.za.netBlogger7125tag:blogger.com,1999:blog-6950106.post-3994614196672521532008-07-12T00:46:00.000Z2008-07-12T00:46:00.000Z2008-07-12T00:46:00.000ZWell spotted Stephen!Well spotted Stephen!Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-81308923493541636562008-07-11T13:05:00.000Z2008-07-11T13:05:00.000Z2008-07-11T13:05:00.000ZThanks for the code, this was just what I was look...Thanks for the code, this was just what I was looking for..<BR/><BR/><BR/>One thing I noticed...<BR/><BR/>if ($lowercase) array_map('strtolower', $array);<BR/><BR/>neeeds to be:<BR/><BR/>if ($lowercase) $array = array_map('strtolower', $array);Stephenhttp://www.blogger.com/profile/03211400871049219751noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-64075193752975930282008-06-16T10:18:00.000Z2008-06-16T10:18:00.000Z2008-06-16T10:18:00.000ZCool thanks Andreas!!Cool thanks Andreas!!Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-58554620695204801322008-06-16T04:14:00.000Z2008-06-16T04:14:00.000Z2008-06-16T04:14:00.000ZThe "advanced" thing, with the following features:...The "advanced" thing, with the following features:<BR/> - give "ABCd" or "AB Cd" or "J E T Word" as an $example_string parameter, to tell the function how it should split acronyms and MIXedWords.. (performance issues are avoided via caching in static variables)<BR/> - additional $glue parameter to implode the array afterwards.<BR/><BR/>See http://www.bevolunteer.org/trac/wiki/CamelCaseExplode<BR/><BR/>Example use:<BR/><BR/>echo '<BR>'.camelCaseExplode('MyXMLParsingEngine', true, 'ABCd', ' ');<BR/>echo '<BR>'.camelCaseExplode('MyXMLParsingEngine', true, 'AB Cd', ' ');<BR/>echo '<BR>'.camelCaseExplode('MyXMLParsingEngine', true, 'A B Cd', ' ');<BR/><BR/>gives<BR/><BR/>My XMLParsing Engine<BR/>My XML Parsing Engine<BR/>My X M L Parsing Engine<BR/><BR/>---------------<BR/><BR/>And here's the code...<BR/>(sorry, blogger doesn't like the < pre > tag in comments)<BR/><BR/> function camelCaseExplode($string, $lowercase = true, $example_string = 'AA Bc', $glue = false)<BR/> {<BR/> static $regexp_available = array(<BR/> '/([A-Z][^A-Z]*)/',<BR/> '/([A-Z][^A-Z]+)/',<BR/> '/([A-Z]+[^A-Z]*)/',<BR/> );<BR/> static $regexp_by_example = array();<BR/> if (!isset($regexp_by_example[$example_string])) {<BR/> $example_array = explode(' ', $example_string);<BR/> foreach ($regexp_available as $regexp) {<BR/> if (implode(' ', preg_split(<BR/> $regexp,<BR/> str_replace(' ', '', $example_string),<BR/> -1,<BR/> PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE<BR/> )) == $example_string) {<BR/> break;<BR/> }<BR/> }<BR/> $regexp_by_example[$example_string] = $regexp;<BR/> }<BR/> $array = preg_split(<BR/> $regexp_by_example[$example_string],<BR/> $string,<BR/> -1,<BR/> PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE<BR/> );<BR/> if ($lowercase) array_map('strtolower', $array);<BR/> return is_string($glue) ? implode($glue, $array) : $array;<BR/> }Andreas H.http://www.blogger.com/profile/09467901533369303116noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-71454620344250416702007-11-12T15:25:00.000Z2007-11-12T15:25:00.000Z2007-11-12T15:25:00.000ZOh yes, just why I didn't design it like that, if ...Oh yes, just why I didn't design it like that, if you join two adjacent segments that are both all-caps one will not know where they should be separated.Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-12451678015468709752007-11-12T15:21:00.000Z2007-11-12T15:21:00.000Z2007-11-12T15:21:00.000ZHey man, good one, thanks for pointing that out! M...Hey man, good one, thanks for pointing that out! My intention was actually that acronyms such as AOL should be split up into 3 elements (that's the way I tested). However, I'm sure lots of people would like to use your solution instead.Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929noreply@blogger.comtag:blogger.com,1999:blog-6950106.post-11725466625423755212007-11-11T22:53:00.000Z2007-11-11T22:53:00.000Z2007-11-11T22:53:00.000ZI think the regex should be /([A-Z][^A-Z]+)/, not ...I think the regex should be /([A-Z][^A-Z]+)/, not /([A-Z][^A-Z]*)/ . Otherwise it splits acronyms like "AOL".Gerghttp://www.blogger.com/profile/16049545665308051296noreply@blogger.com