Use PHP to convert an underscore-delimited string into a camel-named style and capitalize the first letter after the underscore

Source: Internet
Author: User
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 have to do my own clothing and clothing, write two, not much nonsense to say directly paste code:

 $val) $str [$key] = Ucfirst ($val);    if (! $ucfirst) $str [0] = strtolower ($str [0]); Return implode (", $str);}    The first way to call 10w times required $s1 = Microtime_float (); for ($i =0; $i <1000; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline1 ($STR);} $e 1 = microtime_float (); echo ' convertunderline1:run time = '; Echo $e 1-$s 1;echo '
';///The second method calls 10w times $s2 = Microtime_float (); for ($i =0; $i <1000; $i + +) {$str = ' abcd_efgh_igk_lmn '; ConvertUnderline2 ($STR);} $e 2 = Microtime_float (); echo ' convertunderline2:run time = '; Echo $e 2-$s 2;

Why do I have to write two of them? Two different ways of handling it, I want to see that processing efficiency is high.
After testing found that the way string concatenation than with the array to handle 0.1 seconds slower, of course, this is the execution of each function 10w times to see the results, of course, 1w times can see the difference, 1 times can be ignored.
Here are the test results:
Perform 1K times:
Convertunderline1:run time = 0.0050010681152344
Convertunderline2:run time = 0.0039999485015869

Perform 1w times:
Convertunderline1:run time = 0.05500602722168
Convertunderline2:run time = 0.036003828048706

Perform 10w times:
Convertunderline1:run time = 0.46304702758789
Convertunderline2:run time = 0.31903195381165

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 above describes the use of PHP to separate the underlined string into a camel-named style, the first letter after the underscore is capitalized, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.