Use PHP to convert an underscore-delimited string into a camel-named style and capitalize the first letter after the underscore
Recent projects using the Symfony framework, this framework for the operation of the database in this team using ORM to operate, to be honest, the development efficiency and efficiency of using ORM is not necessarily high, to the name of its entity and the name of the existing database field is not the same, ORM entity attributes named Camel-like, Database fields are underlined, which results in the process of field mapping. Field mapping is a headache when it comes to an array that requires manual realism, especially when the field is more than you want to vomit. The problem with this is that the underscore-delimited name field is converted to camel-named. Little brother I also very lazy, in the Internet to find a half-day, also did not find a specific PHP Demo, and some are written in Java, but also quite complex. So I do my own hands-on clothing, handy write a few, nonsense not much to say directly paste code:
$val) $str [$key] = Ucfirst ($val); if (! $ucfirst) $str [0] = strtolower ($str [0]); Return implode (", $str);} Convert the underscore name to the camel-named function ConvertUnderline3 ($str, $ucfirst = True) {$str = Ucwords (Str_replace (' _ ', ' ', $str)); $str = Str_replace ("', '", Lcfirst ($STR)); Return $ucfirst? Ucfirst ($STR): $STR;} Convert the underscore name to the camel-named function convertUnderline4 ($str, $ucfirst = True) {$str = Preg_replace ('/_ ([a-za-z])/E ', "Strtoupper ( ' $ ') ", $str); Return $ucfirst? Ucfirst ($STR): $STR;} Converts an underscore name to a camel-named function convertUnderline5 ($str, $ucfirst = True) {$str = Preg_replace_callback ('/([-_]+ ([A-z]{1})]/ I ', function ($matches) {return Strtoupper ($matches [2]); }, $STR); Return $ucfirst? Ucfirst ($STR): $STR;} $counts = 100000;//The 1th way to call 10w times $S1 = Microtime_float (); for ($i =0; $i < $counts; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline1 ($STR);} $e 1 = microtime_float (); echo ' convertunderline1:run time = '; Echo $e 1-$s 1;echo '
';///2nd Call 10w time Required $s2 = Microtime_float (); for ($i =0; $i < $counts; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline2 ($STR);} $e 2 = Microtime_float (); echo ' convertunderline2:run time = '; Echo $e 2-$s 2;echo '
';///3rd call 10w time Required $s2 = Microtime_float (); for ($i =0; $i < $counts; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline3 ($STR);} $e 2 = Microtime_float (); echo ' convertunderline3:run time = '; Echo $e 2-$s 2;echo '
';///4th call 10w time Required $s2 = Microtime_float (); for ($i =0; $i < $counts; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline4 ($STR);} $e 2 = Microtime_float (); echo ' convertunderline4:run time = '; Echo $e 2-$s 2;echo '
';///5th Call 10w time Required $s2 = Microtime_float (); for ($i =0; $i < $counts; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline4 ($STR);} $e 2 = Microtime_float (); echo ' convertunderline5:run time = '; Echo $e 2-$s 2;echo '
';
Why do I have to write 5 of them? Different treatment methods, I would like to see that the processing efficiency is high.
After testing, the efficiency from high to low method 3> method 2> Method 1> Method 4> Method 5 Of course this is the result that each function executes 10w times can see, of course 1w times also can see difference, 1 times can be negligible.
Here are the test results:
Perform 1K times:
Convertunderline1:run time = 0.005000114440918
Convertunderline2:run time = 0.0040009021759033
Convertunderline3:run time = 0.0039999485015869
Convertunderline4:run time = 0.014001131057739
Convertunderline5:run time = 0.01600193977356
Perform 1w times:
Convertunderline1:run time = 0.056005954742432
Convertunderline2:run time = 0.033003091812134
Convertunderline3:run time = 0.028002977371216
Convertunderline4:run time = 0.14401507377625
Convertunderline5:run time = 0.13701295852661
Perform 10w times:
Convertunderline1:run time = 0.44704508781433
Convertunderline2:run time = 0.32203197479248
Convertunderline3:run time = 0.2670259475708
Convertunderline4:run time = 1.3601360321045
Convertunderline5:run time = 1.3231329917908
Why is string interception concatenation slower than array concatenation? If you look at the bottom of Php C, you'll understand. So later if there are a large number of strings that need to be concatenated into a string, do not connect with points, put them in an array, and use implode to connect.
There is one more thing to say, such a method is also in JS, the array connection is more than the string connection block.
The previous 3 methods are I think out, the back of the regular copy of the cute netizens, but I also quite admire, is a review of the regular.