preg_replace在引用后大写字母

时间:2021-11-03 16:54:19

I have names like this:

我的名字是这样的:

$str = 'JAMES "JIMMY" SMITH'

I run strtolower, then ucwords, which returns this:

我运行strtolower,然后是ucwords,它返回:

$proper_str = 'James "jimmy" Smith'

I'd like to capitalize the second letter of words in which the first letter is a double quote. Here's the regexp. It appears strtoupper is not working - the regexp simply returns the unchanged original expression.

我想把第一个字母是双引号的第二个字母大写。这是正则表达式。似乎strtoupper不起作用 - regexp只返回未更改的原始表达式。

$proper_str = preg_replace('/"([a-z])/',strtoupper('$1'),$proper_str);

Any clues? Thanks!!

有什么线索吗?谢谢!!

7 个解决方案

#1


20  

Use the e modifier to have the substitution be evaluated:

使用e修饰符来评估替换:

preg_replace('/"[a-z]/e', 'strtoupper("$0")', $proper_str)

Where $0 contains the match of the whole pattern, so " and the lowercase letter. But that doesn’t matter since the " doesn’t change when send through strtoupper.

其中$ 0包含整个模式的匹配,所以“和小写字母。但这并不重要,因为”通过strtoupper发送时不会改变。

#2


31  

Probably the best way to do this is using preg_replace_callback():

可能最好的方法是使用preg_replace_callback():

$str = 'JAMES "JIMMY" SMITH';
echo preg_replace_callback('!\b[a-z]!', 'upper', $str);

function upper($matches) {
  return strtoupper($matches[0]);
}

You can use the e (eval) flag on preg_replace() but I generally advise against it. Particularly when dealing with external input, it's potentially extremely dangerous.

您可以在preg_replace()上使用e(eval)标志,但我通常建议不要使用它。特别是在处理外部输入时,它可能非常危险。

#3


18  

Use preg_replace_callback - But you dont need to add an extra named function, rather use an anonymous function.

使用preg_replace_callback - 但是您不需要添加额外的命名函数,而是使用匿名函数。

$str = 'JAMES "JIMMY" SMITH';
echo preg_replace_callback('/\b[a-z]/', function ($matches) {
     return strtoupper($matches[0]);
}, $str);

Use of /e is be deprecated as of PHP 5.5 and doesn't work in PHP 7

从PHP 5.5开始,不推荐使用/ e,在PHP 7中不起作用

#4


0  

Something like this might do the trick:

这样的事情可能会成功:

preg_replace("/(\w+)/e", "ucwords(strtolower('$1'))", $proper_str);

#5


0  

I do this without regex, as part of my custom ucwords() function. Assuming no more than two quotes appear in the string:

我这样做没有正则表达式,作为我的自定义ucwords()函数的一部分。假设字符串中出现的引号不超过两个:

$parts = explode('"', $string, 3); 
if(isset($parts[2])) $string = $parts[0].'"'.ucfirst($parts[1]).'"'.ucfirst($parts[2]);            
else if(isset($parts[1])) $string = $parts[0].'"'.ucfirst($parts[1]);   

#6


0  

You should do this :

你应该做这个 :

$proper_str = 
    preg_replace_callback(
        '/"([a-z])/',
        function($m){return strtoupper($m[1]);},
        $proper_str
);

You should'nt use "eval()" for security reasons.

出于安全原因,您不应该使用“eval()”。

Anyway, the patern modifier "e" is deprecated. See : PHP Documentation.

无论如何,patern修饰符“e”已被弃用。请参阅:PHP文档。

#7


0  

echo ucwords(mb_strtolower('JAMES "JIMMY" SMITH', 'UTF-8'), ' "'); // James "Jimmy" Smith

ucwords() has a second delimiter parameter, the optional delimiters contains the word separator characters. Use space ' ' and " as delimiter there and "Jimmy" will be correctly recognized.

ucwords()有第二个分隔符参数,可选的分隔符包含单词分隔符字符。使用空格''和“作为分隔符,”吉米“将被正确识别。

#1


20  

Use the e modifier to have the substitution be evaluated:

使用e修饰符来评估替换:

preg_replace('/"[a-z]/e', 'strtoupper("$0")', $proper_str)

Where $0 contains the match of the whole pattern, so " and the lowercase letter. But that doesn’t matter since the " doesn’t change when send through strtoupper.

其中$ 0包含整个模式的匹配,所以“和小写字母。但这并不重要,因为”通过strtoupper发送时不会改变。

#2


31  

Probably the best way to do this is using preg_replace_callback():

可能最好的方法是使用preg_replace_callback():

$str = 'JAMES "JIMMY" SMITH';
echo preg_replace_callback('!\b[a-z]!', 'upper', $str);

function upper($matches) {
  return strtoupper($matches[0]);
}

You can use the e (eval) flag on preg_replace() but I generally advise against it. Particularly when dealing with external input, it's potentially extremely dangerous.

您可以在preg_replace()上使用e(eval)标志,但我通常建议不要使用它。特别是在处理外部输入时,它可能非常危险。

#3


18  

Use preg_replace_callback - But you dont need to add an extra named function, rather use an anonymous function.

使用preg_replace_callback - 但是您不需要添加额外的命名函数,而是使用匿名函数。

$str = 'JAMES "JIMMY" SMITH';
echo preg_replace_callback('/\b[a-z]/', function ($matches) {
     return strtoupper($matches[0]);
}, $str);

Use of /e is be deprecated as of PHP 5.5 and doesn't work in PHP 7

从PHP 5.5开始,不推荐使用/ e,在PHP 7中不起作用

#4


0  

Something like this might do the trick:

这样的事情可能会成功:

preg_replace("/(\w+)/e", "ucwords(strtolower('$1'))", $proper_str);

#5


0  

I do this without regex, as part of my custom ucwords() function. Assuming no more than two quotes appear in the string:

我这样做没有正则表达式,作为我的自定义ucwords()函数的一部分。假设字符串中出现的引号不超过两个:

$parts = explode('"', $string, 3); 
if(isset($parts[2])) $string = $parts[0].'"'.ucfirst($parts[1]).'"'.ucfirst($parts[2]);            
else if(isset($parts[1])) $string = $parts[0].'"'.ucfirst($parts[1]);   

#6


0  

You should do this :

你应该做这个 :

$proper_str = 
    preg_replace_callback(
        '/"([a-z])/',
        function($m){return strtoupper($m[1]);},
        $proper_str
);

You should'nt use "eval()" for security reasons.

出于安全原因,您不应该使用“eval()”。

Anyway, the patern modifier "e" is deprecated. See : PHP Documentation.

无论如何,patern修饰符“e”已被弃用。请参阅:PHP文档。

#7


0  

echo ucwords(mb_strtolower('JAMES "JIMMY" SMITH', 'UTF-8'), ' "'); // James "Jimmy" Smith

ucwords() has a second delimiter parameter, the optional delimiters contains the word separator characters. Use space ' ' and " as delimiter there and "Jimmy" will be correctly recognized.

ucwords()有第二个分隔符参数,可选的分隔符包含单词分隔符字符。使用空格''和“作为分隔符,”吉米“将被正确识别。