Code
Result

PHP function to convert a string from CamlCase to snake_case

This is a very simple PHP function (only one single line) to convert a string from CamlCase to snake_case.

All these use cases are handled:

  • simpleTest => simple_test
  • easy => easy
  • HTML => html
  • simpleXML => simple_xml
  • PDFLoad => pdf_load
  • startMIDDLELast => start_middle_last
  • AString => a_string
  • Some4Numbers234 => some4_numbers234
  • TEST123String => test123_string
  • hello_world => hello_world
  • HelloWorld => hello_world
  • helloWorldFoo => hello_world_foo
  • hello_World => hello_world
  • hello-world => hello-world
  • myHTMLFiLe => my_html_fi_le
  • aBaBaB => a_ba_ba_b
  • BaBaBa => ba_ba_ba
  • libC => lib_c