Hello,
Say I have a string that contains "add_product". What would be the regular expression for changing such a string into AddProductAction? In other words, I want to get rid of the underscores and capitalize the first letter of each word.
| SitePoint Sponsor |



Hello,
Say I have a string that contains "add_product". What would be the regular expression for changing such a string into AddProductAction? In other words, I want to get rid of the underscores and capitalize the first letter of each word.
check out
str_replace()
str_replace("add_product", "AddProductAction", $string);



Sorry I should've been more specific. "add_product" was just an example string. How about if the string was "edit_product" or "list_products"? I want to be able to change those last two to "EditProductAction" and "ListProductsAction".
I'm sure there is a better way kinda just hacked it to gather...but it is a start i suppose.PHP Code:$data = 'add_product';
$data = str_replace(' ', '', ucwords(str_replace('_', ' ', $data))) . 'Action';





I'd say
PHP Code:$camel_text = preg_replace(
'/([a-z]+)_([a-z]+)/e',
'ucfirst("$1").ucfirst("$2")."Action"',
$underscores_text);
Bookmarks