簡體   English   中英

使用preg_replace()和regex將大寫字符串替換為小寫

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

是否可以使用preg_replaceregex將大寫字母替換為小寫字母?

例如:

以下字符串:

$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM