简体   繁体   English

将正则表达式与 preg_replace_callback 结合使用

[英]Using of regex with preg_replace_callback

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).我想将可能包含特殊字符的字符串的首字母大写(这就是 ucfirst 在这里无效的原因)。 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]);
}

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.它返回'éSta'但'Ésta'是预期的......我认为我的问题是我正在使用的模式,但我做了不同的组合(比如$pattern = '/\pL/u' )但我没有找到了一个很好的正则表达式。

This is because your az will not match é.这是因为您的az与 é 不匹配。 Writing a regex to encompass unicode characters may be difficult.编写包含 unicode 字符的正则表达式可能很困难。

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);
}

The mb_* functions should handle your special characters correctly. mb_*函数应该正确处理您的特殊字符。


Based on your comment below I understand your dilemma.根据您在下面的评论,我理解您的困境。 In that case you can use your regular expression but with the correct unicode selectors在这种情况下,您可以使用正则表达式,但使用正确的 unicode 选择器

$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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM