drsxzut183207938 2014-04-04 16:03
浏览 18

在paren中使用ucwords的正则表达式

I have some data that I'm looping through and I'm using the below line to transform all caps into mixed caps appropriately:

$str = ucwords(strtolower(trim($str)));

Which is great except that it doesn't work so great within parentheses. So I'm trying to run the below line afterwards to fix that problem, but it's not having any impact. I'm seeing zero change.

$str = preg_replace('/\((.+)\)/e', "ucwords('$0')", $str);

It should turn:

Some Product (with Caps In Paren)

Into:

Some Product (With Caps In Paren)
  • 写回答

3条回答 默认 最新

  • doufang7385 2014-04-04 16:05
    关注

    Use preg_replace_callback instead, using /e modifier is frowned upon (and it's deprecated since PHP 5.5):

    $str = preg_replace_callback('/(?<=\()[^)]+(?=\))/', function($matches) {
        return ucwords(strtolower(trim($matches[0])));
    }, $str);
    

    Demo. Note that I've changed the pattern as well. It now uses lookaround assertions instead of capturing groups.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭