Combine regular expressions for splitting camelCase string into words
Posted
by
stou
on Stack Overflow
See other posts from Stack Overflow
or by stou
Published on 2013-06-28T09:44:50Z
Indexed on
2013/06/28
10:21 UTC
Read the original article
Hit count: 356
I managed to implement a function that converts camel case to words, by using the solution suggested by @ridgerunner in this question:
Split camelCase word into words with php preg_match (Regular Expression)
However, I want to also handle embedded abreviations like this:
'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'
I came up with this solution:
<?php
function camelCaseToWords($camelCaseStr)
{
// Convert: "TestASAPTestMore" to "TestASAP TestMore"
$abreviationsPattern = '/' . // Match position between UPPERCASE "words"
'(?<=[A-Z])' . // Position is after group of uppercase,
'(?=[A-Z][a-z])' . // and before group of lowercase letters, except the last upper case letter in the group.
'/x';
$arr = preg_split($abreviationsPattern, $camelCaseStr);
$str = implode(' ', $arr);
// Convert "TestASAP TestMore" to "Test ASAP Test More"
$camelCasePattern = '/' . // Match position between camelCase "words".
'(?<=[a-z])' . // Position is after a lowercase,
'(?=[A-Z])' . // and before an uppercase letter.
'/x';
$arr = preg_split($camelCasePattern, $str);
$str = implode(' ', $arr);
$str = ucfirst(trim($str));
return $str;
}
$inputs = array(
'oneTwoThreeFour',
'StartsWithCap',
'hasConsecutiveCAPS',
'ALLCAPS',
'ALL_CAPS_AND_UNDERSCORES',
'hasABREVIATIONEmbedded',
);
echo "INPUT";
foreach($inputs as $val) {
echo "'" . $val . "' translates to '" . camelCaseToWords($val). "'\n";
}
The output is:
INPUT'oneTwoThreeFour' translates to 'One Two Three Four'
'StartsWithCap' translates to 'Starts With Cap'
'hasConsecutiveCAPS' translates to 'Has Consecutive CAPS'
'ALLCAPS' translates to 'ALLCAPS'
'ALL_CAPS_AND_UNDERSCORES' translates to 'ALL_CAPS_AND_UNDERSCORES'
'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'
It works as intended.
My question is: Can I combine the 2 regular expressions $abreviationsPattern and camelCasePattern so i can avoid running the preg_split() function twice?
© Stack Overflow or respective owner