使用PHP把下划线分隔定名的字符串 转换成驼峰式命名方式 , 把下划线后面的第一个字母变成大写
使用PHP把下划线分隔命名的字符串 转换成驼峰式命名方式 , 把下划线后面的第一个字母变成大写
最近项目使用symfony框架,这个框架对数据库的操作在这个团队里使用的是ORM进行操作,说实话使用ORM的开发效率和运行效率不一定高多少,到是它的实体命名和现有数据库字段的命名不太一样,ORM实体属性命名是驼峰式的,数据库字段是下划线分隔,这就产生了字段映射的过程。当碰到需要手动写实体必须的数组时,字段映射是一件很头疼的事情,尤其是字段比较多的时候,写到你想吐。到这就产生一个问题就是把以下划线分隔的命名字段转换成驼峰式命名。小弟我也很懒,在网上找了半天,也没找到一个具体的PHP Demo,有的也是java写的,还着还挺复杂。于是乎我就自己动手丰衣足食,顺手写了几个,废话不多说直接贴代码:
<?php//微妙时间function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);}//将下划线命名转换为驼峰式命名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;}//将下划线命名转换为驼峰式命名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);}//将下划线命名转换为驼峰式命名function convertUnderline3 ( $str , $ucfirst = true){ $str = ucwords(str_replace('_', ' ', $str)); $str = str_replace(' ','',lcfirst($str)); return $ucfirst ? ucfirst($str) : $str;}//将下划线命名转换为驼峰式命名function convertUnderline4 ( $str , $ucfirst = true){ $str = preg_replace('/_([A-Za-z])/e',"strtoupper('$1')",$str); return $ucfirst ? ucfirst($str) : $str;}//将下划线命名转换为驼峰式命名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;//第1种方式调用10w次所需时间$s1 = microtime_float();for ($i=0;$i<$counts;$i++){ $str= 'abcd_efgh_igk_lmn'; convertUnderline1($str);}$e1 = microtime_float();echo 'convertUnderline1: run time = ';echo $e1-$s1;echo '<br />';//第2种方式调用10w次所需时间$s2 = microtime_float();for ($i=0;$i<$counts;$i++){ $str= 'abcd_efgh_igk_lmn'; convertUnderline2($str);}$e2 = microtime_float();echo 'convertUnderline2: run time = ';echo $e2-$s2;echo '<br />';//第3种方式调用10w次所需时间$s2 = microtime_float();for ($i=0;$i<$counts;$i++){ $str= 'abcd_efgh_igk_lmn'; convertUnderline3($str);}$e2 = microtime_float();echo 'convertUnderline3: run time = ';echo $e2-$s2;echo '<br />';//第4种方式调用10w次所需时间$s2 = microtime_float();for ($i=0;$i<$counts;$i++){ $str= 'abcd_efgh_igk_lmn'; convertUnderline4($str);}$e2 = microtime_float();echo 'convertUnderline4: run time = ';echo $e2-$s2;echo '<br />';//第5种方式调用10w次所需时间$s2 = microtime_float();for ($i=0;$i<$counts;$i++){ $str= 'abcd_efgh_igk_lmn'; convertUnderline4($str);}$e2 = microtime_float();echo 'convertUnderline5: run time = ';echo $e2-$s2;echo '<br />';
为什么我要写5个呢?不同的处理方式,我是想看看那个处理效率高。
经过测试发现,效率由高到低为 方法3>方法2>方法1>方法4>方法5 当然这是每个函数执行10w次才能看出的结果,当然1w次也能看出差别,1次就可以忽略不计了。
下面是测试结果:
执行1K次:
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
执行1w次:
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
执行10w次:
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
为什么字符串截取拼接要比数组连接慢呢?如果你看过PHP C的底层你就会明白了。所以以后如果有大量的字符串需要连接成一个字符串的 不要在用点连接了,放在一个数组后使用implode连接。
还有一个要说的,这样的方法同样在JS里也是,数组连接要比字符串连接块。
前面3个方法是我想出来的,后面的正则抄袭可爱的网友们的,不过我也是挺佩服的,算是温习了一下正则。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Today we are mainly going to take a look at the time application method of golang time package. The general rule between the two is that "wall time" is used to tell time, and "monotonic clock" is used to measure time; there are other clock processing methods.

1. Overview As part of this article, let us start with some problems with the existing Date and CalendarAPI and explore how the new Java8Date and TimeAPI solve these problems. We will also take a look at the core classes in the Java8 time class library, such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their APIs. 2. The problem of thread safety of the old time API (before Java 8)-Date and Calendar classes are not thread-safe, making it difficult for developers to debug concurrency problems of these APIs and need to write additional code to deal with them.

Pythontime module time acquisition and conversion Python's Time library can perform time-related processing, such as accessing the current date and time, outputting time in different formats, and waiting for a specified time. 1. Get the time 1.1. Timestamp importtimetimestamp=time.time()#1682737552.5009851 Greenwich Mean Time (GMT) The total number of seconds from 00:00:00 on January 1, 1970 to the present 1.2. Structured time importtimestruct_time= time.localtime()#time.struct_time(tm_year=2

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages (such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.

What are the similarities and differences between __str__ and __repr__? We all know the representation of strings. Python's built-in function repr() can express objects in the form of strings to facilitate our identification. This is the "string representation". repr() obtains the string representation of an object through the special method __repr__. If __repr__ is not implemented, when we print an instance of a vector to the console, the resulting string may be. >>>classExample:pass>>>print(str(Example()))>>>

1. Two ways to represent time in Python: timestamp: offset in seconds relative to 1970.1.100:00:00, unique time tuple struct_time: a total of 9 elements>tm_year: year 1-12> tm_mon: month 1-12>tm_mday: day 1-31>tm_hour: hour 0-23>tm_min: minute 0-59>tm_sec: second 0-59>tm_wday: week 0-6 (0 means Sunday)>tm_day: Day of the year 1-366>tm_isdst: whether it is daylight saving, the default is -1.ti

Use the PHP function "time" to return the current UNIX timestamp. The UNIX timestamp refers to the total number of seconds since 0:00:00 on January 1, 1970 Coordinated Universal Time (UTC). In PHP, you can use the built-in function "time" to get the current UNIX timestamp. This article explains how to use this function and provides corresponding code examples. Code example: <?php$timestamp=time();echo" current

Golang is a very popular programming language. Its easy-to-learn, efficient and fast features attract more and more developers. But during use, you will inevitably encounter some problems and errors. For example, when using the After method in the time package, you may encounter an error of undefined: time.After. This article will introduce how to solve this error. Understand the cause of the error In Golang, if we use an unexported function name or incorrect
