Home  >  Article  >  Backend Development  >  PHP 中 正则表达式转化小写上划线变量变驼峰式

PHP 中 正则表达式转化小写上划线变量变驼峰式

PHP中文网
PHP中文网Original
2017-03-30 17:13:551585browse

  PHP 中 正则表达式转化小写下划线变量变驼峰式
   

$method = 'set'.preg_replace(
      "/(?:^|_)([a-z])/e", 
             "strtoupper('\\1')", 
             $f);

 first_name -> setFirstName
   click_url -> setClickUrl

正则中的/e可以执行. 此特性在perl中也是存在的.

如果不可以执行那只能

a -> A
 b -> B
 ...
 z -> Z

不知道还有没有别的办法.

以上就是PHP 中 正则表达式转化小写上划线变量变驼峰式的内容,更多相关内容请关注PHP中文网(www.php.cn)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn