您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在PHP中将短横线转换为驼峰命名法

2024/3/17 14:48:19发布23次查看
以下是将破折号转换为驼峰命名法的php代码 −
示例输入 − this-is-a-test-string
示例输出 − thisisateststring
注意 − 不需要使用正则表达式或回调函数,可以使用ucwords函数实现。
function dashtocamelcase($string, $capitalizefirstcharacter = false) { $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string))); if (!$capitalizefirstcharacter) { $str[0] = strtolower($str[0]); } return $str;}echo dashtocamelcase('this-is-a-string');
for php version>=5.3, the below code can be used −
function dashtocamelcase($string, $capitalizefirstcharacter = false) { $str = str_replace('-', '', ucwords($string, '-')); if (!$capitalizefirstcharacter) { $str = lcfirst($str); } return $str;echo dashtocamelcase('this-is-a-test-string');
the 'lcfirst' function needs to be used instead of 'strtolower'.
以上就是在php中将短横线转换为驼峰命名法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product