2013-03-08 61 views
-5

我有一个类似“kp_o_zmq_k”的字符串,我需要将它转换为“kpOZmqK”,我需要将连接到下划线右侧的所有字母(o,z,k在这种情况下)改为大写。将所有下划线连接的字母转换为大写php

+0

你的意思是,你想要将具有前面下划线的字母转换为大写? – Brad 2013-03-08 04:16:12

+0

因为简单的'连接'意味着p和q也应该是大写的。 – aqua 2013-03-08 04:17:01

+0

你想到了什么?你有什么尝试? – ultranaut 2013-03-08 04:17:34

回答

3

尝试使用preg_replace_callback功能在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], "_")); 
} 
+0

谢谢Justing ...这对我有效! – Abhiraj 2013-03-08 06:57:19

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

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

    return $return; 
} 
?> 
相关问题