使用 preg_replace() 和正则表达式将大写字符串替换为小写字符串
Replace an upper-case string with lower-case using preg_replace() and regex
是否可以使用 preg_replace
和 regex
将大写字母替换为小写字母?
例如:
以下字符串:
$x="HELLO LADIES!";
我想将其转换为:
hello ladies!
使用 preg_replace()
:
echo preg_replace("/([A-Z]+)/","",$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
Regex101 演示:https://regex101.com/r/tD7sI0/1
如果你只是想让整个字符串小写,而不是只使用 strtolower
整个字符串。
是否可以使用 preg_replace
和 regex
将大写字母替换为小写字母?
例如:
以下字符串:
$x="HELLO LADIES!";
我想将其转换为:
hello ladies!
使用 preg_replace()
:
echo preg_replace("/([A-Z]+)/","",$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
Regex101 演示:https://regex101.com/r/tD7sI0/1
如果你只是想让整个字符串小写,而不是只使用 strtolower
整个字符串。