简体   繁体   English

在PHP中需要一些正则表达式的帮助

[英]Need help with some regular expressions in PHP

I need to port some simple eregi regular expressions to preg_match for PHP 5.3/6.0 compilance. 我需要将一些简单的eregi正则表达式移植到preg_match以便进行PHP 5.3 / 6.0编译。

Since I'm not too confident in my regular expression porting skills I need some help... 由于我对正则表达式的移植技巧不太自信,因此我需要一些帮助...

#1 #1

Old version: 旧版本:

if( eregi('foo',$myVar) ) {
    $aresult = explode('/',stristr($myVar,'foo'));
    $aversion = explode(' ',$aresult[1]);
}

New version: 新版本:

if( preg_match('/Foo\/([^ ]*)/i',$myVar,$matches) ) {
    $aversion = $matches[1];
}

#2 #2

Old version: 旧版本:

if( eregi('bar',$myVar) && ! eregi('rv:[0-9]\.[0-9]\.[0-9]',$myVar)) {
    $aresult = explode('/',stristr($myVar,'bar'));
    $aversion = explode(' ',$aresult[1]);
}

New version: 新版本:

//Not done yet need help

Your second snippet is pretty much the same as the first, just with that extra condition. 您的第二个代码段与第一个代码段几乎相同,只是有额外的条件。 I'm going to guess that your actual code (or how you want it to work) is a little different to that presented? 我猜测您的实际代码(或您希望它的工作方式)与所提供的代码有些不同? If so, could you elaborate on those differences please? 如果是这样,请您详细说明这些差异?

Either way, your #2 could look similar to #1. 无论哪种方式,您的#2看起来都可能与#1相似。

if (preg_match('~bar/([^ ]*)~i', $myVar, $match) && ! preg_match('/rv:[0-9]\.[0-9]\.[0-9]/', $myVar)) {
    $aversion = $match[1];
}

The use of ~ as the delimiters might seem strange; 使用~作为分隔符似乎很奇怪; the reasoning being that the regular expression contains the most usual delimiting character ( / ) so an alternative is used instead of escaping the slash as you did in the question. 原因是正则表达式包含最常用的定界字符( / ),因此可以使用替代方法而不是像在问题中那样转义斜线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM