Convert Camel Case Pascal Case to Snake Case in PHP Example

Sovary October 13, 2022 671
2 minutes read

Today article we will explain how to convert letter in Camel case Pascal case to Snake case letter in pure PHP or in Laravel as well. We are going to convert camel case letter to snake case. Next we will explain what different and transform either letter Camel case or Pascal Case into Snake case with below examples.

Camel case refer to multiple words come close together without space where first word is lower case then the next words are start from capital letter. Pascal case is pretty similar to Camel case where each words join together with no space and each word are start with capital letter. Snake case refer to words replace space with underscore. We will give you example to distinct between Camel case, Snake case, Pascal case.

You may get idea what writing style should be with the name Camel and Snake. Camel case which refer to capital letters resemble the humps on a camel's back whereas, Snake case where underscores representing snakes that slither on the ground between each words.

We will show you very short example how to convert those Camel case to snake case as well as Pascal case to Snake case. Please see below example with output sample:

Camel Case to Snake Case

"getNameEmployees" => "get_name_employees"
"getAllStudentRecords" => "get_all_student_records"

Pascal Case to Snake Case

"GetNameEmployees" => "get_name_employees"
"GetStudentRecords" => "get_student_records"

Now let's see how we can convert with PHP, we will give you a few different examples so you can choose which one is the best for you.

Example #1

<?php

function camelToSnake($text)
{
    return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $text));
}
  
echo camelToSnake('getNameEmployees');
echo camelToSnake('getAllStudentRecords');

Output #1:

get_name_employees
get_all_student_records

Example #2

<?php

function toSnakeCase($text)
{
    return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $text));
}
  
echo toSnakeCase('GETNameEmployees');
echo toSnakeCase('Queen8Records');
echo toSnakeCase('updateRecord');

Output #2:

get_name_employees
queen8_records
update_record

Example #3

<?php

function toSnakeCase($text) 
{
  $pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
  preg_match_all($pattern, $text, $matches);
  $ret = $matches[0];
  foreach ($ret as &$match) 
  {
    $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
  }
  return implode('_', $ret);
}

echo toSnakeCase("sunRAISEEast");
echo toSnakeCase("Sum9Numbers45");
echo toSnakeCase("simpleHtmlDom");

Output #3:

sun_raise_east
sum9_numbers45
simple_html_dom

Hope this short example help you to transform Camel case Pascal case into Snake case. Have a nice day!

You might also like..

 

PHP 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him