Use PHP to convert the strings separated by underlines into the camper naming method, and change the first letter after the underline into upper case and hump underline
Recently, the project uses the symfony framework. This framework operates databases using ORM in this team. To be honest, the development efficiency and running efficiency of Using ORM are not necessarily high, the object name is not the same as the existing database field name. The ORM object attribute name is in the hump format, and the database fields are separated by underscores, which leads to the field ing process. When you need to manually write an array required by an object, field ing is a headache, especially when there are many fields. This leads to the problem of converting the namefield separated by the following strip into a camper name. I am also very lazy, I have been searching for the Internet for a long time, and I have not found a specific PHP Demo. Some of them are also written in java, which is still quite complicated. As a result, I made a lot of effort and wrote two things by myself. I don't need to talk much nonsense and paste the Code directly:
<? Php // subtle time function microtime_float () {list ($ usec, $ sec) = explode ("", microtime (); return (float) $ usec + (float) $ sec) ;}// convert the underline name to the hump name function convertUnderline1 ($ str, $ ucfirst = true) {while ($ pos = strpos ($ str, '_'))! = False) $ str = substr ($ str, 0, $ pos). ucfirst (substr ($ str, $ pos + 1); return $ ucfirst? Ucfirst ($ str): $ str;} // convert the underline name to the hump name function convertUnderline2 ($ str, $ ucfirst = true) {$ str = explode ('_', $ str); foreach ($ str as $ key => $ val) $ str [$ key] = ucfirst ($ val ); if (! $ Ucfirst) $ str [0] = strtolower ($ str [0]); return implode ('', $ str );} // The first method calls 1000 times the required time $ s1 = microtime_float (); for ($ I = 0; $ I <; $ I ++) {$ str = 'abcd _ efgh_igk_lmn '; convertUnderline1 ($ str);} $ e1 = microtime_float (); echo 'convertunderline1: run time ='; echo $ e1-$ s1; echo '<br/>'; // The second method calls 1000 times the required time $ s2 = microtime_float (); for ($ I = 0; $ I <; $ I ++) {$ str = 'abcd _ efgh_igk_lmn '; convertUnderline2 ($ str) ;}$ e2 = microtime_float (); echo 'convertunderline2: run time = '; echo $ e2-$ s2;
Why should I write two? Two different processing methods: I want to see if the processing efficiency is high.
After testing, it is found that the String concatenation method is 0.1 seconds slower than Array Processing. Of course, this is the result that can be seen after every function is executed for times. Of course, the difference can be seen after times, it can be ignored once.
The test result is as follows:
1 K executions:
ConvertUnderline1: run time = 0.0050010681152344
ConvertUnderline2: run time = 0.0039999485015869
1 million executions:
ConvertUnderline1: run time = 0.05500602722168
ConvertUnderline2: run time = 0.036003828048706
Execute 10 million times:
ConvertUnderline1: run time = 0.46304702758789
ConvertUnderline2: run time = 0.31903195381165
Why is string truncation and splicing slower than array connection? If you have read the bottom layer of php c, you will understand it. Therefore, if a large number of strings need to be connected to a single string, do not use point connection. Put the strings in an array and use implode connection.
Another thing to say is that this method is also in JS, and array connection is better than string connection block.