正则表达式适用于某些服务器,而不适用于其他服务器


Regular expression works on some servers and not others

我在调试正则表达式时遇到问题。首先,代码(这是完整的文件...很抱歉缺少换行符 - 请参阅 http://pastebin.com/h5CeiY5F 的粘贴):

<?php
$matches = null;
$returnValue = preg_match('#" FirstDownType="[A-Z][0-9]+"/><Play PlayDescription="Penalty[^<]+/>#', '<Play DownDistanceYardline="3-1-GB 7" EarnedFirstDown="False" PlayDescription="(3:40) 71-C.Brown reported in as eligible. 28-M.Ingram left guard to GB 7 for no gain (94-J.Wynn; 95-H.Green)."/><Play DownDistanceYardline="4-1-GB 7" EarnedFirstDown="False" PlayDescription="(3:10) 9-D.Brees pass incomplete short left to 23-P.Thomas."/><Play Header="Green Bay Packers at 3:02"/><Play DownDistanceYardline="1-10-GB 7" EarnedFirstDown="False" PlayDescription="(3:02) 44-J.Starks right tackle to GB 11 for 4 yards (94-C.Jordan)."/><Play DownDistanceYardline="2-6-GB 11" EarnedFirstDown="False" PlayDescription="(2:26) 12-A.Rodgers pass deep right to 85-G.Jennings pushed ob at GB 33 for 22 yards (33-J.Greer)." FirstDownType="P17"/><Play PlayDescription="Penalty on NO-33-J.Greer, Defensive Pass Interference, declined."/><Play DownDistanceYardline="1-10-GB 33" EarnedFirstDown="True" PlayDescription="(2:01) (Shotgun) 12-A.Rodgers pass short left to 85-G.Jennings to GB 47 for 14 yards (21-P.Robinson)." FirstDownType="P18"/><Play DownDistanceYardline="1-10-GB 47" EarnedFirstDown="True" PlayDescription="(1:22) 12-A.Rodgers pass short right to 87-J.Nelson pushed ob at NO 44 for 9 yards (27-M.Jenkins)."/><Play DownDistanceYardline="2-1-NO 44" EarnedFirstDown="False" PlayDescription="(:47) 44-J.Starks right tackle to NO 42 for 2 yards (51-J.Vilma; 94-C.Jordan)." FirstDownType="R19"/><Play DownDistanceYardline="1-10-NO 42" EarnedFirstDown="True" PlayDescription="(:07) 25-R.Grant right tackle to NO 40 for 2 yards (51-J.Vilma, 58-S.Shanle)."/><QuarterSummary Team="New Orleans Saints" Score="27" TimeOfPossession="10:47" FirstDownsRushing="3" FirstDownsPassing="5" FirstDownsPenalty="1" FirstDownsTotal="9" ThirdDownEfficiency="1/3" FourthDownEfficiency="0/1"/><QuarterSummary Team="Green Bay Packers" Score="35" TimeOfPossession="4:13" FirstDownsRushing="1" FirstDownsPassing="2" FirstDownsPenalty="0" FirstDownsTotal="3" ThirdDownEfficiency="0/1" FourthDownEfficiency="0/0"/>', $matches);
print_r($matches);

当我在几个沙箱(如 http://sandbox.onlinephpfunctions.com/或 functions-online.com/preg_match.html)上运行它时,它会返回:

Array ( [0] => " FirstDownType="P17"/><Play PlayDescription="Penalty on NO-33-J.Greer, Defensive Pass Interference, declined."/> )

这就是我正在寻找的预期输出。

但是,当我在我的服务器上运行它时(我已经在两个不同的服务器上测试了它),我得到:

Array ( [0] => " FirstDownType="P17"/> )

我能想到的只是preg_match PHP 5.3.10(沙盒上的版本)和 PHP 5.3.6(我们的版本)之间发生了变化,或者我们的 Ubuntu 版本配置错误?

我真的很感激任何帮助。谢谢!

你需要使用正则表达式来匹配它吗? 改用 XML 解析器怎么样?

尝试使用 SimpleXML 获取所需的节点。

$sXML = new SimpleXMLElement('<xml>'.$xml.'</xml>');

然后,您可以使用 XPath 查找所需的元素。

$play = $sXML->xpath('//Play[starts-with(@PlayDescription, "Penalty")]/preceding-sibling::Play[@FirstDownType]');

这将选择Play元素前面的Play元素,该元素的PlayDescription"Penalty" 开头。

演示:http://codepad.viper-7.com/ECQKcB

相关文章: