简体   繁体   English

使用preg_replace()和regex将大写字符串替换为小写

[英]Replace an upper-case string with lower-case using preg_replace() and regex

Is it possible to replace an upper-case with lower-case using preg_replace and regex ? 是否可以使用preg_replaceregex将大写字母替换为小写字母?

For example: 例如:

The following string: 以下字符串:

$x="HELLO LADIES!";

I want to convert it to: 我想将其转换为:

 hello ladies!

using preg_replace() : 使用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 Regex101演示: 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. 如果只希望整个字符串都小写,而不是仅在整个字符串上使用strtolower

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM