Camel name and underline name Mutual transfer PHP implementation

Source: Internet
Author: User

Hump naming and underline naming often require mutual transfer, the following provides two ways to implement PHP.
The first method is relatively inefficient and is implemented in the following ways:

//Hump named turn underline named    functionTounderscore ($str)    {        $dstr=Preg_replace_callback('/([a-z]+)/',function($matchs)        {            return‘_‘.Strtolower($matchs[0]); },$str); return Trim(Preg_replace('/_{2,}/', ' _ ',$dstr),‘_‘); }    //underline name to hump name    functionTocamelcase ($str)    {        $array=Explode(‘_‘,$str); $result=$array[0]; $len=Count($array); if($len>1)        {             for($i= 1;$i<$len;$i++)            {                $result.=Ucfirst($array[$i]); }        }        return $result; } Diego Link: http://www.jianshu.com/p/773fd334052fSource: Jane Book copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. 

The second method is more ingenious and efficient, and the second method is recommended

/** * underline to HUMP * Idea: * Step1. The original string is converted to lowercase, the delimiter in the original string is replaced with a space, and a delimiter is added at the beginning of the string * Step2. Converts the first letter of each word in the string to uppercase, then to the space, to the string header appended by the delimiter.*/    functionCamelize ($uncamelized _words,$separator= ' _ ')    {        $uncamelized _words=$separator.Str_replace($separator, " ",Strtolower($uncamelized _words)); return LTrim(Str_replace(" ", "",Ucwords($uncamelized _words)),$separator ); }/** * Hump named Turn underline name * idea: * Lowercase and uppercase close together place, plus delimiter, and then all turn lowercase*/    functionUncamelize ($camelCaps,$separator= ' _ ')    {        return Strtolower(Preg_replace('/([A-z]) ([A-z])/', "$".$separator. "$",$camelCaps)); } Diego Link: http://www.jianshu.com/p/773fd334052fSource: Jane Book copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. 

Camel name and underline name Mutual transfer PHP implementation

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.