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

发布于 2024-11-14 05:28:45 字数 763 浏览 2 评论 0原文

由于 POSIX 正则表达式 (ereg) 自 PHP 5.3.0 起已弃用,我想知道一种简单的方法将旧表达式转换为 PCRE(Perl 兼容正则表达式)(preg)

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

eregi('^hello world');

How can Itranslate statements into preg_match Compatible Expressions?

注意:此帖子充当与从 ereg 到 preg 转换相关的所有帖子的占位符,并作为相关问题的重复选项。请不要关闭此帖子问题。

相关:

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).

Per example, I have this regular expression:

eregi('^hello world');

How can I translate expressions into preg_match compatible expressions?

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:

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

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

发布评论

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

评论(4

又爬满兰若 2024-11-21 05:28:45

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

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

分隔符几乎可以是字母数字、反斜杠或空白字符之外的任何内容。最常用的一般是~/#

您还可以使用匹配括号:

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

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

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

您可以使用 preg_quote

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

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

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

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

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

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

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:

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

Also, PCRE supports modifiers for various things. One of the most used is the case-insensitive modifier i, the alternative to 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 differences between POSIX regex and PCRE to help converting the expression.

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

stripos($str, 'hello world') === 0
青春有你 2024-11-21 05:28:45

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

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

将 ereg 模式移植到 preg 时,您应该了解 4 个主要知识:

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

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

    .+

    ';
    $new_pattern = '/' 。 addcslashes($old_pattern, '/') 。 '/';

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

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

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

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

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

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

  2. Escape delimiter if 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.

画中仙 2024-11-21 05:28:45

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

ereg 迁移到 preg_match 只是我们模式的一个小变化。

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

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

to

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

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

eregi('pattern', 'string');

to

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

From PHP version 5.3, ereg is deprecated.

Moving from ereg to preg_match is just a small change in our pattern.

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 eregi case-insensitive matching, put i after the last delimiter, e.g.:

eregi('pattern', 'string');

to

preg_match ('/pattern/i', 'string');
玩物 2024-11-21 05:28:45

ereg()preg_replace() 之间除了语法之外还有更多差异:

  • 返回值:

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

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

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

  • 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.

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.

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