[英]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]+)/","$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
Regex101演示: https ://regex101.com/r/tD7sI0/1
如果只希望整个字符串都小写,而不是仅在整个字符串上使用strtolower
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.