转义字符串以在正则表达式中使用

发布于 2024-10-16 07:59:37 字数 684 浏览 2 评论 0原文

可能的重复:
是否有 PHP可以在应用正则表达式模式之前对其进行转义的函数吗?

我想在正则表达式中使用存储在变量中的字符串。 (在 PHP 中)转义字符串以便在 (PCRE) 正则表达式中使用的最佳方法是什么?

例如,

$text = 'm/z';  // this is the text I want to use as part of my regular expression
$text_regexp = '#' . $text . '#i';  // this is my regular expression

会给出

$text_regexp = '#m/z#i';

But I would Want 以下正则表达式:

$text_regexp = '#m\/z#i';

这是一个人为的示例,但我想简单地说明这一点。

Possible Duplicate:
Is there a PHP function that can escape regex patterns before they are applied?

I want to use a string stored in a variable in a regular expression. What's the best way (in PHP) to escape a string for use in a (PCRE) regular expression?

For example,

$text = 'm/z';  // this is the text I want to use as part of my regular expression
$text_regexp = '#' . $text . '#i';  // this is my regular expression

Would give

$text_regexp = '#m/z#i';

But I would want the following regular expression:

$text_regexp = '#m\/z#i';

This is a contrived example, but I wanted to illustrate the point simply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-10-23 07:59:37

preg_quote

来自手册:

在每个前面加一个反斜杠
属于常规字符的一部分
表达式语法

您还可以将分隔符作为第二个参数传递,它也会被转义。但是,如果您使用 # 作为分隔符,则无需转义 /

preg_quote

From the manual:

puts a backslash in front of every
character that is part of the regular
expression syntax

You can also pass the delimiter as the second parameter and it will also be escaped. However, if you're using # as your delimiter, then there's no need to escape /

雨落星ぅ辰 2024-10-23 07:59:37

您必须使用 preg_quote 函数

preg_quote ( string $str [, string $delimiter = NULL ] )

必须作为整个单词匹配的 $keyword 示例:

$pattern = '/\b' . preg_quote($keyword, '/') . '\b/';

You must use the preg_quote function:

preg_quote ( string $str [, string $delimiter = NULL ] )

Example of a $keyword that must match as a whole word:

$pattern = '/\b' . preg_quote($keyword, '/') . '\b/';
弥繁 2024-10-23 07:59:37

Check this. You can use preg_quote();.

烧了回忆取暖 2024-10-23 07:59:37

所有这些答案都是正确的,只有元字符需要转义。
可惜 php 没有像 preg_match([operator]/$var/) 这样的正则表达式运算符,因此分隔符可以作为引用代理。这样 delimeter 就不必在外部 $var 中引用。 (Perl 有 m//,s/// 或 =~ 运算符)

preg_quote 是一个很好的方法,不幸的是,有时你想匹配文字 '\char' 而不将其转义为 '\char'实例,并逃避其余的。 preg_quote() 的选择不多。

您可以从字面上设置自己的 php 函数来引用元字符(或在 preg_quote() 之后去掉引号)。

就像将所有 /([{}\[\]()^\$.|*+?\\<>$delim])/ 替换为 \\$1或其他东西。设置 $delim,和/或删除可能的转义符或任何其他您不想转义的元字符。

只是我的 $.02

All these answers are correct, only the metachars need to be escaped.
Its kind of too bad that php doesen't have a regex operator like preg_match([operator]/$var/) so the delimeters could be the quoting agent. This way delimeter wouldn't have to be quoted in an external $var. (Perl has this with m//,s/// or =~ operators)

The preg_quote is a nice way to do this, unfortunately, sometimes you want to match literal '\char' without it being escaped to '\char' for instance, and escape the rest. Not many choices with preg_quote().

You could literally set up your own php functions that does quoting of metachars (or take off a quote after preg_quote() ).

like a replace all /([{}\[\]()^\$.|*+?\\<>$delim])/ with \\$1 or something. Set the $delim, and/or take out possibly the escape or any other metachars you don't want escaped.

Just my $.02

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文