将preg_replace()e修饰符替换为preg_replace_callback [英] Replace preg_replace() e modifier with preg_replace_callback

查看:186
本文介绍了将preg_replace()e修饰符替换为preg_replace_callback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很讨厌正则表达式.我正在尝试取代它:

I'm terrible with regular expressions. I'm trying to replace this:

public static function camelize($word) {
   return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);
}

带有带有匿名函数的preg_replace_callback.我不明白\\ 2在做什么.或就此而言,preg_replace_callback的工作方式完全一样.

with preg_replace_callback with an anonymous function. I don't understand what the \\2 is doing. Or for that matter exactly how preg_replace_callback works.

实现此目标的正确代码是什么?

What would be the correct code for achieving this?

推荐答案

在正则表达式中,您可以使用(brackets)捕获"匹配字符串的一部分;在这种情况下,您正在捕获匹配的(^|_)([a-z])部分.这些从1开始编号,因此您有向后引用1和2.匹配0是整个匹配的字符串.

In a regular expression, you can "capture" parts of the matched string with (brackets); in this case, you are capturing the (^|_) and ([a-z]) parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.

/e修饰符采用替换字符串,并用适当的反引用替换反斜杠后跟数字(例如\1)-但由于您位于字符串中,因此需要转义反斜杠,因此您得到'\\1'.然后,它(有效地)运行eval来运行结果字符串,就好像它是PHP代码一样(这就是为什么不推荐使用它的原因,因为它很容易以不安全的方式使用eval).

The /e modifier takes a replacement string, and substitutes backslash followed by a number (e.g. \1) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\\1'. It then (effectively) runs eval to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval in an insecure way).

preg_replace_callback函数改为使用回调函数,并将包含匹配的反向引用的数组传递给它.因此,在您要编写'\\1'的位置,您改为访问该参数的元素1-例如如果您具有格式为function($matches) { ... }的匿名函数,则第一个向后引用是该函数内部的$matches[1].

The preg_replace_callback function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\\1', you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) { ... }, the first back-reference is $matches[1] inside that function.

所以

'do_stuff(\\1) . "and" . do_stuff(\\2)'

可能成为

function($m) { return do_stuff($m[1]) . "and" . do_stuff($m[2]); }

或者您的情况

'strtoupper("\\2")'

有可能成为

function($m) { return strtoupper($m[2]); }

请注意,$m$matches并不是魔术名称,它们只是我在声明回调函数时提供的参数名称.另外,您不必传递匿名函数,它可以是字符串形式的函数名,也可以是array($object, $method)

Note that $m and $matches are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method), as with any callback in PHP, e.g.

function stuffy_callback($things) {
    return do_stuff($things[1]) . "and" . do_stuff($things[2]);
}
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');

与任何函数一样,默认情况下,您无法访问回调之外的变量(来自周围的作用域).使用匿名函数时,可以使用use关键字导入需要访问的变量,

As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was

'do_stuff(\\1, $foo)'

然后新的回调可能看起来像

then the new callback might look like

function($m) use ($foo) { return do_stuff($m[1], $foo); }

陷阱

  • 使用preg_replace_callback代替正则表达式中的/e修饰符是,因此您需要从模式"参数中删除该标志.因此,像/blah(.*)blah/mei这样的模式将变成/blah(.*)blah/mi.
  • /e修饰符内部在参数上使用了addslashes()的变体,因此某些替换使用stripslashes()删除了它.在大多数情况下,您可能希望从新的回调中删除对stripslashes的调用.
  • Gotchas

    • Use of preg_replace_callback is instead of the /e modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like /blah(.*)blah/mei would become /blah(.*)blah/mi.
    • The /e modifier used a variant of addslashes() internally on the arguments, so some replacements used stripslashes() to remove it; in most cases, you probably want to remove the call to stripslashes from your new callback.
    • 这篇关于将preg_replace()e修饰符替换为preg_replace_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆