php – 如何在句子中用括号括起来

我使用以下代码来大写句子中的每个单词,但我无法将附加括号的单词大写.

PHP代码

  <?PHP
     $str = "[this is the {command line (interface ";
     $output  = ucwords(strtolower($str));
     echo $output;

输出

[这是{命令行(界面

但我的预期输出应该是:

[这是{命令行(接口

如何处理带括号的单词?
可能有多个括号.

例如:

[{this is the({command line({(interface

我想在PHP中找到一个通用的解决方案/函数.

解决方法:

$output = ucwords($str, ' [{(');
echo $output;
// output ->
// [This Is The {Command Line (Interface

更新:一般解决方案.这里的“括号” – 是任何非字母字符. “括号”后面的任何字母都会转换为大写.

$string = "test is the {COMMAND line -STRET (interface 5more 9words #here";
$strlowercase = strtolower($string);

$result = preg_replace_callback('~(^|[^a-zA-Z])([a-z])~', function($matches)
{
    return $matches[1] . ucfirst($matches[2]);
}, $strlowercase);


var_dump($result);
// string(62) "Test Is The {Command Line -Stret (Interface 5More 9Words #Here"

直播demo

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...