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

后端 未结 1 1829
生来不讨喜
生来不讨喜 2020-12-31 12:24

Is it possible to replace an upper-case with lower-case using preg_replace and regex?

For example:

The following string:

         


        
相关标签:
1条回答
  • 2020-12-31 13:23

    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.

    0 讨论(0)
提交回复
热议问题