Bench-marked against 3 alternatives, I believe your method is the fastest. Here's the results from 100,000 iterations:
array(4) {
["Test1"]=>
float(0.23144102096558)
["Test2"]=>
float(0.41140103340149)
["Test3"]=>
float(0.31215810775757)
["Test4"]=>
float(0.98423790931702)
}
Where Test1
is yours, Test2
and Test3
are mine, and Test4
is from @RizwanMTuman's answer (with a fix).
I thought using preg_split
may give you an opportunity to optimise. In this function, only 1 regex is used and returns an array of only the alpha items to which you then apply ucfirst
to:
function FoxJourneyLikeACamelsHump_2(string $string): string {
return implode('', array_map(function($word) {
return ucfirst($word);
}, preg_split("/[^[:alpha:]]/", $string, null, PREG_SPLIT_NO_EMPTY)));
}
This can be further optimised by using foreach
instead of array_map
(see here):
function FoxJourneyLikeACamelsHump_3(string $string): string {
$validItems = preg_split("/[^[:alpha:]]/u", $string, null, PREG_SPLIT_NO_EMPTY);
$result = '';
foreach($validItems as $item) {
$result .= ucfirst($item);
}
return $result;
}
This leads me to speculate that 2 regexes and 1 ucwords
is faster than 1 regex and multiple ucfirst
s.
Full test script:
<?php
// yours
function FoxJourneyLikeACamelsHump_1(string $string): string {
$string = preg_replace("/[^[:alpha:][:space:]]/u", ' ', $string);
$string = ucwords($string);
$camelCase = preg_replace('/\s+/', '', $string);
return $camelCase;
}
// mine v1
function FoxJourneyLikeACamelsHump_2(string $string): string {
return implode('', array_map(function($word) {
return ucfirst($word);
}, preg_split("/[^[:alpha:]]/", $string, null, PREG_SPLIT_NO_EMPTY)));
}
// mine v2
function FoxJourneyLikeACamelsHump_3(string $string): string {
$validItems = preg_split("/[^[:alpha:]]/u", $string, null, PREG_SPLIT_NO_EMPTY);
$result = '';
foreach($validItems as $item) {
$result .= ucfirst($item);
}
return $result;
}
// Rizwan with a fix
function FoxJourneyLikeACamelsHump_4(string $string): string {
$re = '/(?:\b|\d+)([a-z])|[\d+ +!.@]/';
$result = preg_replace_callback($re,function ($matches) {
return (isset($matches[1]) ? strtoupper($matches[1]) : '');
},$string);
return $result;
}
// $expected = "ThQuCkBrWnFXJumpsVRThLZyDG";
$test1 = 0;
$test2 = 0;
$test3 = 0;
$test4 = 0;
$loops = 100000;
$time_start = microtime(true);
for($i=0; $i<$loops; $i++) {
$string = " Th3 qu!ck br0wn f0x jumps 0v3r th3 l@zy d0g. ";
$is = FoxJourneyLikeACamelsHump_1($string);
if($loops==1) echo $is."\n";
}
$time_end = microtime(true);
$test1 = $time_end - $time_start;
$time_start = microtime(true);
for($i=0; $i<$loops; $i++) {
$string = " Th3 qu!ck br0wn f0x jumps 0v3r th3 l@zy d0g. ";
$is = FoxJourneyLikeACamelsHump_2($string);
if($loops==1) echo $is."\n";
}
$time_end = microtime(true);
$test2 = $time_end - $time_start;
$time_start = microtime(true);
for($i=0; $i<$loops; $i++) {
$string = " Th3 qu!ck br0wn f0x jumps 0v3r th3 l@zy d0g. ";
$is = FoxJourneyLikeACamelsHump_3($string);
if($loops==1) echo $is."\n";
}
$time_end = microtime(true);
$test3 = $time_end - $time_start;
$time_start = microtime(true);
for($i=0; $i<$loops; $i++) {
$string = " Th3 qu!ck br0wn f0x jumps 0v3r th3 l@zy d0g. ";
$is = FoxJourneyLikeACamelsHump_4($string);
if($loops==1) echo $is."\n";
}
$time_end = microtime(true);
$test4 = $time_end - $time_start;
var_dump(array('Test1'=>$test1, 'Test2'=>$test2, 'Test3'=>$test3, 'Test4'=>$test4));