使用 preg_replace 和 regex 能否将大写字母替换成小写字母?
例如:
下面的字符串:
$x="HELLO LADIES!";
我想把它转换为:
hello ladies!
使用preg_replace()
函数:
echo preg_replace("/([A-Z]+)/","$1",$x);
使用 preg_replace 和 regex 能否将大写字母替换成小写字母?
例如:
下面的字符串:
$x="HELLO LADIES!";
我想把它转换为:
hello ladies!
使用preg_replace()
函数:
echo preg_replace("/([A-Z]+)/","$1",$x);
我认为这是你想要实现的目标:
$x="HELLO LADIES! This is a test";
echo preg_replace_callback('/\b([A-Z]+)\b/', function ($word) {
return strtolower($word[1]);
}, $x);
输出:
hello ladies! This is a test
正则表达式101演示:https://regex101.com/r/tD7sI0/1
如果您只是想将整个字符串转为小写,那么只需对整个字符串使用strtolower
。
preg_replace
来完成这个任务,因为没有模式可用。为什么不使用strtolower
呢? - Alexandre Leprêtrestrtolower()
是不是一个过于简单的解决方案? - RiggsFolly