简体   繁体   English

正则表达式将大写变为小写

[英]Regex turn upper to lower-case

I am trying to change it all to lower case letters.我正在尝试将其全部更改为小写字母。 It works if the word is all uppercase, but if only 1 letter in the word is uppercase it does not change it.如果单词全部大写,它会起作用,但如果单词中只有 1 个字母是大写的,它不会改变它。 I would really appreciate it if someone fixed my code.如果有人修复了我的代码,我将不胜感激。

<?php
$x = "HELLO WORLD! THIS is a test";
echo preg_replace_callback('/\b([A-Z]+)\b/', function($word) {
   return strtolower($word[1]);
}, $x);
?>

\b matches a word-boundary. \b 匹配单词边界。 So you can achieve your goal just omit \b's in your regex.所以你可以在你的正则表达式中省略 \b's 来实现你的目标。 Try this:尝试这个:

<?php
$x = "HELLO WORLD! THIS is a test";
echo preg_replace_callback('/([A-Z]+)/', function($word) {
    return strtolower($word[1]);
}, $x);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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