问题
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?
回答1:
These are always fun puzzles to solve; I've narrowed down the cases to just two:
Detect words that start with a capital followed by lowercase letters (but not preceded by a word boundary or start of the subject) -
(?<!\b)[A-Z][a-z]+
Detect transitions from lowercase to uppercase -
(?<=[a-z])[A-Z]
function camelFix($str) { return preg_replace_callback('/(?<!\b)[A-Z][a-z]+|(?<=[a-z])[A-Z]/', function($match) { return ' '. $match[0]; }, $str); }
It works for the inputs you have given; it might fail cases that I have not foreseen :)
来源:https://stackoverflow.com/questions/17361768/combine-regular-expressions-for-splitting-camelcase-string-into-words