php - 使用 preg_replace() 和正则表达式将大写字符串替换为小写字符串

标签 php regex preg-replace

是否可以使用 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

关于php - 使用 preg_replace() 和正则表达式将大写字符串替换为小写字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34420366/

相关文章:

php preg_replace ,我需要替换点后的所有内容(例如 340.38888 > 需要清理 340)

php - Laravel dompdf 错误 "Image not found or type unknown"

php - PHP 中的父类(super class)和子类

PHP:如何创建 randomDate($startDate, $endDate) 函数?

iphone - iphone sdk 应用程序的最佳正则表达式库?

php - 如何执行 preg_replace() 删除除 "Post"或 "Posts"之外的任何内容?

php - "Cannot convert value to string"取数据时(belongsToMany关系)

javascript - 制作与 Javascript 字符串匹配的 Regex 对象。讲解方法

java - 我怎样才能在Java中分割这个字符串?

php - 即使没有协议(protocol),也将 url 从文本转换为链接