What would be the PHP equivalent of this Perl regex?
if (/^([a-z0-9-]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/
and $开发者_如何学Go1 ne "global" and $1 ne "") {
print " <tr>\n";
print " <td>$1</td>\n";
print " <td>$2</td>\n";
print " <td>$3</td>\n";
print " <td>$4</td>\n";
print " <td>$5</td>\n";
print " <td>$6</td>\n";
print " <td>$7</td>\n";
print " <td>$8</td>\n";
print " </tr>\n";
}
I'd suggest that rather than using a regex, you split on whitespace. All you're checking for is eight columns separated by whitespace.
Look at preg_split
at http://www.php.net/manual/en/function.preg-split.php. It should look something like:
$fields = preg_split( '/\s+/', $string );
if ( $fields[0] == '...' )
...
preg_match
PHP has some functions that work with PCRE. So try this:
if (preg_match('/^([a-z0-9-]+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/', $str, $match) && $match[1] != "global" && $match[1] != "") {
print " <tr>\n";
print " <td>$match[1]</td>\n";
print " <td>$match[2]</td>\n";
print " <td>$match[3]</td>\n";
print " <td>$match[4]</td>\n";
print " <td>$match[5]</td>\n";
print " <td>$match[6]</td>\n";
print " <td>$match[7]</td>\n";
print " <td>$match[8]</td>\n";
print " </tr>\n";
}
精彩评论