Converting all underscore connected letters to uppercase in php [closed]

≯℡__Kan透↙ 提交于 2019-12-13 11:24:49

问题


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.


回答1:


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], "_"));
}



回答2:


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

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

  return $return;
}
?>


来源:https://stackoverflow.com/questions/15286610/converting-all-underscore-connected-letters-to-uppercase-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!