Question

I have a string like "kp_o_zmq_k" and I need to covert it to "kpOZmqK" where I need to convert all letters connected to the right of the underscore(o,z,k in this case) to uppercase.

Was it helpful?

Solution 2

Try with preg_replace_callback function in php.

$ptn = "/_[a-z]?/";
$str = "kp_o_zmq_k";
$result = preg_replace_callback($ptn,"callbackhandler",$str);
// print the result
echo $result;

function callbackhandler($matches) {
    return strtoupper(ltrim($matches[0], "_"));
}

OTHER TIPS

<?php
function underscore2Camelcase($str) {
  // Split string in words.
  $words = explode('_', strtolower($str));

  $return = '';
  foreach ($words as $word) {
    $return .= ucfirst(trim($word));
  }

  return $return;
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top