HomeWeb application programmingPHP programmingPHP: Camel Case to Spaces or Underscore

PHP: Camel Case to Spaces or Underscore

Recently while working on a project, I found I needed to convert camelcase strings to space-separated strings. This same technique will work if you want to convert camelcase to underscore separation as well.

Camel case is frequently used in programming and data storage. Here are a couple of ways to convert it. As usual, feel free to leave better methods in the comments below.

CamelCase to Underscore–

//example: parseServerInfo to parse_server_info
$string = preg_replace(‘/(?<=\\w)(?=[A-Z])/',"_$1", $string); $string = strtolower($string); CamelCase to Spaces–

//example:BachJS to Bach J S
$string = preg_replace(‘/(?<=\\w)(?=[A-Z])/'," $1", $string); $string = trim($string);

David Kirk
David Kirk
David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

Cleveland Furia on How to Clear Google Activity
Sid Wohlfarth on How to Clear Google Activity
Riley Magsayo on How to Clear Google Activity
สมัครรับเครดิตฟรี ทันที on How to improve Remote Desktop Protocol Performance
Neal Wangstad on How to Clear Google Activity
Slavcho Andov on How to install Ubuntu
Slavcho Andov on How to fix bad sectors
Sheraz Ali on How to Backup Android