Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php paren中带ucwords的正则表达式_Php_Regex - Fatal编程技术网

Php paren中带ucwords的正则表达式

Php paren中带ucwords的正则表达式,php,regex,Php,Regex,我有一些循环使用的数据,我正在使用下面的行将所有大写转换为混合大写: $str = ucwords(strtolower(trim($str))); 这很好,除了括号内的效果不太好。因此,我试图在之后运行下面的代码来解决这个问题,但它没有任何影响。我看到零变化 $str = preg_replace('/\((.+)\)/e', "ucwords('$0')", $str); 它应该转向: 某些产品(带括号中的大写) 进入: 某些产品(带括号中的大写) 改为使用,不赞成使用/e修饰符(从P

我有一些循环使用的数据,我正在使用下面的行将所有大写转换为混合大写:

$str = ucwords(strtolower(trim($str)));
这很好,除了括号内的效果不太好。因此,我试图在之后运行下面的代码来解决这个问题,但它没有任何影响。我看到零变化

$str = preg_replace('/\((.+)\)/e', "ucwords('$0')", $str);
它应该转向:

某些产品(带括号中的大写)
进入:

某些产品(带括号中的大写)
改为使用,不赞成使用
/e
修饰符(从PHP 5.5开始就不推荐使用):

$str=preg\u replace\u回调('/(?如果扩展名可用,请使用所需的转换

$str = mb_convert_case($str, MB_CASE_TITLE, "ASCII");
转换为:

如果需要,请指定。

如果“在括号内不起作用”,您的意思是它无法在括号内获取单词,您可以使用可选的修饰符:

$str = ucwords(strtolower("My string (with Caps in parens)"), '( ');

这将使它把每个空间和括号都看作是新词的开始,并将它大写。

我尝试了这个模式,但是它删除了括号中的所有内容,并给了我两组括号,这显然不是我想要的。当然,正如你在演示中看到的,它是有效的。你确定你照抄了代码吗?关于示例字符串和输出?我不仅觉得Yaakov的解决方案更干净,我相信使用
~\(\K[^)]+(?=\)~
会更有效。这样正则表达式引擎只需查找
字符,然后用
\K
释放它,然后开始与您的模式进行匹配,该模式将逐步通过每个非
字符,然后每次向后查看前一个字符是否为
如果为true,则开始匹配。
mb_
函数的速度非常慢。除非没有它,情况无法得到适当解决,否则应避免使用
mb_
函数。否则,一个好的答案肯定会对多字节场景有所帮助。
Some Product (With Caps In Paren)
$str = ucwords(strtolower("My string (with Caps in parens)"), '( ');