Home > Backend Development > PHP Tutorial > 正则表达式 - PHP如何将字符串中带有“-”的地方去掉“-”并将后面紧跟的字母转换为大写

正则表达式 - PHP如何将字符串中带有“-”的地方去掉“-”并将后面紧跟的字母转换为大写

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:43:22
Original
1273 people have browsed it


“ask-simple”转换为“askSimple”
“ask-simple-answer”转换为“askSimpleAnswer”

回复内容:


“ask-simple”转换为“askSimple”
“ask-simple-answer”转换为“askSimpleAnswer”

function camelcase($str){
    return    preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
        return strtoupper($matches[2]);
    },$str);
}
Copy after login

来一个正则表达式版本的

preg_replace('/-([A-Za-z])/e',"strtoupper('$1')",$str)
Copy after login

<code>function camelCase($value){

     $studly = ucwords(str_replace(array('-', '_'), ' ', $value));
     return str_replace(' ','',lcfirst($studly));
}

echo camelCase('ask-simple-answer');
</code>
Copy after login

“ask-simple-answer”转换为“askSimpleAnswer”

这个应该是camel case, 刚好想起laravel有这个函数, 稍作修改,抄过来了

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template