Skip to content

Instantly share code, notes, and snippets.

@SmetDenis
Last active August 29, 2015 14:27
Show Gist options
  • Save SmetDenis/1c8246afe883a34d63ae to your computer and use it in GitHub Desktop.
Save SmetDenis/1c8246afe883a34d63ae to your computer and use it in GitHub Desktop.
<?php
/**
* @param string $input
* @return mixed|string
*/
function CamelCase2Human($input)
{
$original = $input;
if (strpos($input, '\\') !== false) {
$input = explode('\\', $input);
reset($input);
$input = end($input);
}
if (!preg_match('#^tests#i', $input)) {
$input = preg_replace('#^(test)#i', '', $input);
}
$input = preg_replace('#(test)$#i', '', $input);
$output = preg_replace(array('/(?<=[^A-Z])([A-Z])/', '/(?<=[^0-9])([0-9])/'), ' $0', $input);
$output = ucwords($output);
$output = trim($output);
if (strcasecmp($original, $output) === 0) {
return $original;
}
if (strlen($output) == 0) {
return $original;
}
return $output;
}
$tests = array(
'LocalBusiness' => 'Local Business',
'CivicStructureBuilding' => 'Civic Structure Building',
'getUserMobilePhoneNumber' => 'Get User Mobile Phone Number',
'bandGuitar1' => 'Band Guitar 1',
'band2Guitar123' => 'Band 2 Guitar 123',
'CustomerIDWithSomeOtherJETWords' => 'Customer IDWith Some Other JETWords',
'testCustomerIDWithSomeOtherJETWords' => 'Customer IDWith Some Other JETWords',
'TESTCustomerIDWithSomeOtherJETWords' => 'Customer IDWith Some Other JETWords',
'TestCustomerIDWithSomeOtherJETWords' => 'Customer IDWith Some Other JETWords',
'SmetDenis\\SimpleTypes\\PerformanceTest' => 'Performance',
'SmetDenis\\SimpleTypes\\testPerformanceTest' => 'Performance',
'Tests suite' => 'Tests suite',
'Tests test' => 'Tests',
'Test test' => 'Test test',
);
foreach ($tests AS $input => $expected) {
$output = CamelCase2Human($input);
echo $input . ' : '.($output === $expected ? 'OK' : '<strong style="color:red;">fail</strong> ' . $expected ) . '<br>';
echo '<pre>' . $output . '</pre><hr/>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment