Replace lowercase string with preg_replace () and regex

Is it possible to replace upper case with lower case with preg_replaceand regex?

For example:

Next line:

$x="HELLO LADIES!";

I want to convert it to:

 hello ladies!

with the help of preg_replace():

 echo preg_replace("/([A-Z]+)/","$1",$x);
+4
source share
1 answer

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 line to be lowercase, but just use strtolowerin everything.

+9
source

Source: https://habr.com/ru/post/1621309/


All Articles