如何在 PHP 中将 ereg 表达式转换为 preg?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6270004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:06  来源:igfitidea点击:

How can I convert ereg expressions to preg in PHP?

phppreg-replacepreg-matchpcreereg

提问by netcoder

Since POSIX regular expressions (ereg)are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg).

由于自 PHP 5.3.0 起不推荐使用POSIX 正则表达式 (ereg),我想知道一种将旧表达式转换为PCRE (Perl Compatible Regular Expressions) (preg) 的简单方法

Per example, I have this regular expression:

例如,我有这个正则表达式:

eregi('^hello world');

How can I translate expressions into preg_matchcompatible expressions?

如何将表达式转换为preg_match兼容的表达式?

Note:This post serves as a placeholder for all posts related to conversion from ereg to preg, and as a duplicate options for related questions.Please do not close this question.

Related:

注意:这篇文章用作所有与从 ereg 到 preg 转换相关的帖子的占位符,并作为相关问题的重复选项。请不要关闭这个问题。

有关的:

回答by netcoder

The biggest change in the syntax is the addition of delimiters.

语法中最大的变化是添加了分隔符

ereg('^hello', $str);
preg_match('/^hello/', $str);

Delimiters can be pretty much anything that is not alpha-numeric, a backslash or a whitespace character. The most used are generally ~, /and #.

分隔符几乎可以是任何不是字母数字、反斜杠或空格字符的东西。最常用的一般是~,/#

You can also use matching brackets:

您还可以使用匹配的括号:

preg_match('[^hello]', $str);
preg_match('(^hello)', $str);
preg_match('{^hello}', $str);
// etc

If your delimiter is found in the regular expression, you have to escape it:

如果在正则表达式中找到了分隔符,则必须对其进行转义:

ereg('^/hello', $str);
preg_match('/^\/hello/', $str);

You can easily escape all delimiters and reserved characters in a string by using preg_quote:

您可以使用preg_quote轻松转义字符串中的所有分隔符和保留字符:

$expr = preg_quote('/hello', '/');
preg_match('/^'.$expr.'/', $str);

Also, PCRE supports modifiersfor various things. One of the most used is the case-insensitive modifier i, the alternative to eregi:

此外,PCRE 支持各种修饰符。最常用的一个是不区分大小写的修饰符i,它是eregi的替代品:

eregi('^hello', 'HELLO');
preg_match('/^hello/i', 'HELLO');

You can find the complete reference to PCRE syntax in PHP in the manual, as well as a list of differencesbetween POSIX regex and PCRE to help converting the expression.

您可以在手册中找到对PHP 中 PCRE 语法的完整参考,以及POSIX regex 和 PCRE 之间的差异列表,以帮助转换表达式。

However, in your simple example you would not use a regular expression:

但是,在您的简单示例中,您不会使用正则表达式:

stripos($str, 'hello world') === 0

回答by Sumoanand

Ereg replacement with preg(as of PHP 5.3.0) was right move in our favor.

用 preg 替换 Ereg(自 PHP 5.3.0 起)是对我们有利的正确举措。

preg_match, which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg.

preg_match 使用与 Perl 兼容的正则表达式语法,通常是 ereg 的更快替代方案。

You should know 4 main things to port ereg patterns to preg:

您应该知道将 ereg 模式移植到 preg 的 4 个主要事项:

  1. Add delimiters(/): 'pattern' => '/pattern/'

  2. Escape delimiterif it is a part of the pattern: 'patt/ern' => '/patt\/ern/'
    Achieve it programmatically in following way:
    $old_pattern = '<div>.+</div>';
    $new_pattern = '/' . addcslashes($old_pattern, '/') . '/';

  3. eregi(case-insensitive matching): 'pattern' => '/pattern/i'So, if you are using eregi function for case insenstive matching, just add 'i' in the end of new pattern('/pattern/').

  4. ASCII values: In ereg, if you use number in the pattern, it is assumed that you are referring to the ASCII of a character. But in preg, number is not treated as ASCII value. So, if your pattern contain ASCII value in the ereg expression(for example: new line, tabs etc) then convert it to hexadecimal and prefix it with \x.
    Example: 9(tab) becomes \x9 or alternatively use \t.

  1. 添加分隔符(/):'pattern' => '/pattern/'

  2. 如果它是模式的一部分,则转义分隔符'patt/ern' => '/patt\/ern/'
    通过以下方式以编程方式实现它:
    $old_pattern = '<div>.+</div>';
    $new_pattern = '/' . addcslashes($old_pattern, '/') . '/';

  3. eregi(不区分大小写的匹配):'pattern' => '/pattern/i'因此,如果您使用 eregi 函数进行不区分大小写的匹配,只需在新模式('/pattern/')的末尾添加 'i'。

  4. ASCII 值:在 ereg 中,如果您在模式中使用数字,则假定您指的是字符的 ASCII。但在 preg 中,数字不被视为 ASCII 值。因此,如果您的模式在 ereg 表达式中包含 ASCII 值(例如:换行、制表符等),则将其转换为十六进制并以 \x 为前缀。
    Example: 9(tab) becomes \x9 or alternatively use \t.

回答by Narayan

From PHP version 5.3, eregis deprecated.

从 PHP 5.3 版开始,ereg已弃用。

Moving from eregto preg_matchis just a small change in our pattern.

eregpreg_match只是我们模式的一个小变化。

First, you have to add delimiters to your code, e.g.:

首先,您必须在代码中添加分隔符,例如:

ereg('A-Z0-9a-z', 'string');

to

preg_match('/A-Z0-9a-z/', 'string');

For eregicase-insensitive matching, put iafter the last delimiter, e.g.:

对于eregi不区分大小写的匹配,放在i最后一个分隔符之后,例如:

eregi('pattern', 'string');

to

preg_match ('/pattern/i', 'string');

回答by Roman Hocke

There are more differences between ereg()and preg_replace()than just the syntax:

还有更多的之间的差异ereg(),并preg_replace()不仅仅是语法:

  • Return value:

    • On error: both return FALSE
    • On no match: ereg()returns FALSE, preg_match()returns 0
    • On match: ereg()returns string length or 1, preg_match()returns always 1
  • Resulting array of matched substrings: If some substring is not found at all ((b)in ...a(b)?), corresponding item in ereg()result will be FALSE, while in preg_match()it will not be set at all.

  • 返回值:

    • 出错时:两者都返回FALSE
    • 不匹配时ereg()返回FALSEpreg_match()返回0
    • 匹配时ereg()返回字符串长度或1preg_match()始终返回1
  • 匹配子字符串的结果数组:如果根本找不到某个子字符串((b)in ...a(b)?),则ereg()结果中的相应项将为FALSE,而 inpreg_match()根本不会设置。

If one is not brave enough to convert his or her ereg()to preg_match(), he or she may use mb_ereg(), which is still available in PHP 7.

如果没有足够的勇气将他或她转换ereg()preg_match(),他或她可以使用mb_ereg(),它在 PHP 7 中仍然可用。