...

View Full Version : preg_replace for capitalising?



mlse
12-01-2006, 01:25 PM
Hi all,

I want to convert a string like "this_is_my_string" to "thisIsMyString".

The closest I've got is:



$mystr = "this_is_my_string";
print(preg_replace('{\_([a-z])}', 'strtoupper("$1")', $mystr));


Which doesn't quite work! (try it). I need to get preg_replace to recognise the call to strtoupper, or some equivalent method.

Any ideas?

TIA,
Mike.

GJay
12-01-2006, 01:49 PM
$new_string = str_replace(' ','',strtoupper(str_replace('_',' ',$mystr)));

will do it

chump2877
12-01-2006, 02:00 PM
I dont know how to do that with regex, but this works:


$mystr = "this_is_my_string";
$mystr_arr = explode("_",$mystr);
foreach ($mystr_arr as $k => $v) {
if ($k != 0)
$mystr_arr[$k] = ucfirst($v);
}
$mystr = implode("",$mystr_arr);

print $mystr;

marek_mar
12-01-2006, 02:08 PM
$mystr = 'this_is_my_string';
print preg_replace_callback('/_([a-z])/', create_function('$a', 'return strtoupper($a[1]);'), $mystr)

mlse
12-01-2006, 03:19 PM
GJay: errr ... no! Thanks anyway.

chump2877: Thanks, but I was looking for a preg_replace solution.

marek_mar: Joy! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum