将字符串的第一个字母大写(前面有特殊字符)- PHP

4
3个回答

2

使用 preg_replace_callback 函数,如上所述,但同时兼容 Unicode:

echo preg_replace_callback('/^(\PL*)(\pL)/u', function($matches){
    return $matches[1] . mb_strtoupper($matches[2],'UTF-8');
}, '¿"éllo"?'),"\n";

输出:

¿"Éllo"?

1

您可以使用preg_replace_callback来完成:

preg_replace_callback('/^([^a-z]*)([a-z])/i', function($matches){
    return $matches[1] . strtoupper($matches[2]);
}, '¿"hello"?');

// ¿"Hello"?

这已经接近解决方案了,现在我认为我遇到了正则表达式的问题,因为当我的字符串是'¿ésta prueba?'时,这个函数返回'¿ésta prueba?',但是期望的是'¿Ésta prueba?'... - jprog
这里是最终解决方案的发布位置: http://stackoverflow.com/questions/11133485/using-of-regex-whith-preg-replace-callback/11133597#comment14592579_11133597 - jprog

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接