Reputation: 943
I am replacing the first_name_user
with 'firstNameUser' in php.
I am trying this
$key1 = preg_replace('/_([a-z]?)/', '\U\1', $key);
and its not working
Upvotes: 2
Views: 1769
Reputation: 9302
Alternate to @roblll answer, if you don't want to use regex, you could use:
$str = "some_underscored_text";
$new_str = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
echo $new_str; // Outputs: someUnderscoredText
Upvotes: 0
Reputation: 8821
$key1 = preg_replace('/_([a-z]?)/e', 'strtoupper("$1")', $key);
But this uses the e
modifier which has some issues security implications and is deprecated as of PHP 5.5.0 anyways.
Use of this modifier is discouraged, as it can easily introduce security vulnerabilites
Better use:
$key1 = preg_replace_callback('/_([a-z]?)/', function($match) {
return strtoupper($match[1]);
}, $key);
See this question and this question for more examples / code and information.
Upvotes: 3