简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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