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

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

PHP中文网
Release: 2023-02-28 13:12:01
Original
1669 people have browsed it

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

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

 first_name -> setFirstName
   click_url -> setClickUrl
Copy after login

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

如果不可以执行那只能

a -> A
 b -> B
 ...
 z -> Z
Copy after login

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

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template