使用 preg_replace() 和 regex 用小写替换大写字符串 [英] Replace an upper-case string with lower-case using preg_replace() and regex
本文介绍了使用 preg_replace() 和 regex 用小写替换大写字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 preg_replace
和 regex
将大写替换为小写?
例如:
以下字符串:
$x="HELLO LADIES!";
我想将其转换为:
女士们你好!
使用preg_replace()
:
echo preg_replace("/([A-Z]+)/","$1",$x);
解决方案
我认为这就是您要实现的目标:
$x="HELLO LADIES!这是一个测试";echo preg_replace_callback('/\b([A-Z]+)\b/', function ($word) {返回 strtolower($word[1]);}, $x);
输出:
你好女士们!这是一个测试
Regex101 演示:https://regex101.com/r/tD7sI0/1>
如果你只是想让整个字符串小写,而不是只在整个事情上使用 strtolower
.
Is it possible to replace an upper-case with lower-case using preg_replace
and regex
?
For example:
The following string:
$x="HELLO LADIES!";
I want to convert it to:
hello ladies!
using preg_replace()
:
echo preg_replace("/([A-Z]+)/","$1",$x);
解决方案
I think this is what you are trying to accomplish:
$x="HELLO LADIES! This is a test";
echo preg_replace_callback('/\b([A-Z]+)\b/', function ($word) {
return strtolower($word[1]);
}, $x);
Output:
hello ladies! This is a test
Regex101 Demo: https://regex101.com/r/tD7sI0/1
If you just want the whole string to be lowercase though than just use strtolower
on the whole thing.
这篇关于使用 preg_replace() 和 regex 用小写替换大写字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文