How to parse Camel Case to human readable string?
Asked Answered
T

2

16

Is it possible to parse camel case string in to something more readable.

for example:

  • LocalBusiness = Local Business
  • CivicStructureBuilding = Civic Structure Building
  • getUserMobilePhoneNumber = Get User Mobile Phone Number
  • bandGuitar1 = Band Guitar 1

UPDATE

Using simshaun regex example I managed to separate numbers from text with this rule:

function parseCamelCase($str)
{
    return preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]|[0-9]{1,}/', ' $0', $str);
}

//string(65) "customer ID With Some Other JET Words With Number 23rd Text After"
echo parseCamelCase('customerIDWithSomeOtherJETWordsWithNumber23rdTextAfter');
Thekla answered 6/6, 2011 at 15:9 Comment(0)
M
34

There are some examples in the user comments of str_split in the PHP manual.

From Kevin:

<?php
$test = 'CustomerIDWithSomeOtherJETWords';

preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $test);


And here's something I wrote to meet your post's requirements:

<?php
$tests = array(
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
);

foreach ($tests AS $input => $expected) {
    $output = preg_replace(array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/'), ' $0', $input);
    $output = ucwords($output);
    echo $output .' : '. ($output == $expected ? 'PASSED' : 'FAILED') .'<br>';
}
Milla answered 6/6, 2011 at 15:13 Comment(4)
I think you need to wrap the output from preg_replace with trim to remove the space that it prepends to the string.Polymorphous
@simshaun. Test passes if acronym is inside string but fails when string begins or ends with acronym. thanksMadelina
Bug in yuor code: CustomerIDWithSomeOtherJETWords => Customer IDWith Some Other JETWordsXerosere
As others have indicated, this answer is not 100% reliable. 3v4l.org/1KFKMMariano
M
0

Here is my take on trying to parse PascalCase and camelCase strings into space-delimited titlecase. The in-pattern comments should help to describe what each subpattern is doing.

Code: (Demo)

$tests = [
    'LocalBusiness' => 'Local Business',
    'CivicStructureBuilding' => 'Civic Structure Building',
    'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
    'bandGuitar1' => 'Band Guitar 1',
    'band2Guitar123' => 'Band 2 Guitar 123',
    'CustomerIDWithSomeOtherJETWords' => 'Customer ID With Some Other JET Words',
    'noOneIsMightierThanI' => 'No One Is Mightier Than I',
    'USAIsNumber14' => 'USA Is Number 14',
    '99LuftBallons' => '99 Luft Ballons',
];

$result = [];
foreach ($tests as $input => $expected) {
    $newString = ucwords(
        preg_replace(
            '/(?:
               [A-Z]+?         (?=\d|[A-Z][a-z])  #acronyms
               |[A-Z]?[a-z]+   (?=[^a-z])         #words
               |\d+            (?=\D)             #numbers
               |(*SKIP)(*FAIL)                    #abort
              )\K
             /x',
            ' ',
            $input
        )
    );
    $result[] = ($newString === $expected ? 'PASSED' : 'FAILED') . ': "' . $newString . '"';
}
var_export($result);
  • acronyms are found by matching 1 or more uppercase letters followed by either a number or a word.
  • words are found by matching lowercase letters which may or may not be preceded by an uppercase letter.
  • numbers are found by matching 1 or more digits which are followed by a non-digit.
  • anything that does not match should be prohibited from qualifying for space injection.
Mariano answered 31/8, 2023 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.