Use PHP to convert the strings separated by underlines to the camper naming method, and change the first letter after the underline to uppercase.

Source: Internet
Author: User
: This article mainly introduces how to use PHP to convert strings separated by underlines into a hump naming method, and convert the first letter after the underline into uppercase letters, if you are interested in the PHP Tutorial, refer. 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:

 $ 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'
'; // 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.

The preceding section describes how to use PHP to convert the strings separated by underlines into a hump naming method, and convert the first letter after the underline into uppercase letters, including content, if you are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.