我想利用类似的字符串:大写字符串的第一个字母(前面有特殊字符) - PHP
¿"hello"?
我希望我的函数返回
¿"Hello"?
我已经试着用正则表达式和的preg_match,没有运气... 这是我刚才的问题,与此相关的一个: "preg_match is matching two characters when it should only match one"
谢谢大家!
我想利用类似的字符串:大写字符串的第一个字母(前面有特殊字符) - PHP
¿"hello"?
我希望我的函数返回
¿"Hello"?
我已经试着用正则表达式和的preg_match,没有运气... 这是我刚才的问题,与此相关的一个: "preg_match is matching two characters when it should only match one"
谢谢大家!
可以使用preg_replace_callback做到这一点:
preg_replace_callback('/^([^a-z]*)([a-z])/i', function($matches){
return $matches[1] . strtoupper($matches[2]);
}, '¿"hello"?');
// ¿"Hello"?
尝试ucfirst
功能http://php.net/manual/en/function.ucfirst.php
是需要这样的任务没有正则表达式
样品
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
使用preg_replace_callback作为所述ASCII上面的时间,但兼容Unicode:
echo preg_replace_callback('/^(\PL*)(\pL)/u', function($matches){
return $matches[1] . mb_strtoupper($matches[2],'UTF-8');
}, '¿"éllo"?'),"\n";
输出:“?¿ESTA prueba”
¿"Éllo"?
这是解决近,现在我觉得我在与正则表达式,因为当我的字符串是问题,这个函数返回'éstaprueba?'但是'Éstaprueba?'预计... – jprog
这是最终的解决方案发布: http://stackoverflow.com/questions/11133485/using-of-regex-whith-preg-replace-callback/11133597#comment14592579_11133597 – jprog