Convert from underscore_style to camelCaseStyle and Vice Versa

  // AStringConverted -> a_string_converted
  function toUnderscoreStyle($s) {
    $f = function ($match) { return "_" . mb_strtolower($match[1]); };
    return preg_replace_callback('/([A-Z])/', $f, lcfirst($s));
  }

  // a_string_converted -> aStringConverted
  function toCamelCase($s) {
    $f = function($match) { return mb_strtoupper($match{1}); };
    return preg_replace_callback('/_([a-z])/', $f, $s);
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.