在 preg_replace_callback 中使用正则表达式 [英] Using of regex with preg_replace_callback
问题描述
我想将可能包含特殊字符的字符串的第一个字母大写(这就是 ucfirst 在这里无效的原因).我有下一个代码:
I'd like to capitalize the first letter of a string which could have special characters (that's the reason ucfirst is not valid here). I have next code:
$string = 'ésta';
$pattern = '/^([^a-z]*)([a-z])/i';
$callback_fn = 'process';
echo preg_replace_callback($pattern, $callback_fn, $string);
function process($matches){
return $matches[1].strtoupper($matches[2]);
}
返回 'éSta' 但 'Ésta' 是预期的......我认为我的问题是我使用的模式,但我做了不同的组合(比如 $pattern = '/\pL/u'
) 但我还没有找到一个好的正则表达式.
which returns 'éSta' but 'Ésta' was expected... I think my problem is the pattern I'm using, but I have made different combinations (like $pattern = '/\pL/u'
) but I haven't found a good regex.
推荐答案
这是因为您的 a-z
与 é 不匹配.编写包含 unicode 字符的正则表达式可能很困难.
This is because your a-z
will not match é. Writing a regex to encompass unicode characters may be difficult.
从您的代码中,它只会将第一个字母大写,而不管您的字符串中有多少单词.如果是这样就这样做:
From your code it will only capitalise the first letter, regardless of the amount of words in your string. If so just do this:
$string = 'ésta';
$ucstring = ucphrase($string);
function ucphrase($word) {
return mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
}
mb_*
函数应该正确处理您的特殊字符.
The mb_*
functions should handle your special characters correctly.
根据您在下面的评论,我理解您的困境.在这种情况下,您可以使用正则表达式,但要使用正确的 unicode 选择器
Based on your comment below I understand your dilemma. In that case you can use your regular expression but with the correct unicode selectors
$string = 'ésta';
$pattern = '/(\p{L})(.+)/iu';
$callback_fn = 'process';
echo preg_replace_callback($pattern, $callback_fn, $string);
function process($matches){
return mb_strtoupper($matches[1], 'UTF-8') . $matches[2];
}
这篇关于在 preg_replace_callback 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!