2012-07-06 70 views
0

正在調試正則表達式時遇到問題。首先,該代碼(這是完整的文件...對不起缺乏換行符 - 見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/或功能,在線運行此它返回:

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

這是我正在尋找的預期輸出。

然而,當我在我的服務器上運行它(我已經測試了兩臺不同的服務器),我得到:

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

所有我能想到的是PHP 5.3.10之間的變化的preg_match (沙箱上的版本)和PHP 5.3.6(我們的版本),還是我們的Ubuntu版本配置錯誤?

我非常感謝任何幫助。謝謝!

+1

如果您不能懶得添加這些換行符,我爲什麼要費心去從引擎收錄下載並找到了這個錯誤? – 2012-07-06 16:48:50

+1

它確實匹配 - 但我必須查看源代碼才能看到完整匹配,因爲瀏覽器試圖將其解析爲HTML。另外,你真的不應該試圖通過REGEX解析標籤結構。使用DOM方法。 – Utkanos 2012-07-06 16:56:02

+1

@Utkanos - 耐心等待+1。爲什麼不能有人問一個沒有水平滾動條的問題? – 2012-07-06 16:58:37

回答

1

你需要使用正則表達式匹配嗎?如何使用XML解析器來代替?

嘗試使用SimpleXML來獲取所需的節點。

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

然後你可以使用XPath找到你所需要的元素(一個或多個)。

​​

這將選擇具有PlayDescription開始"Penalty"Play元件之前的Play元件。

DEMO:http://codepad.viper-7.com/ECQKcB

+0

我實際上使用DOM進行大部分處理......但我試圖先對它進行預處理。這就是爲什麼我在這裏使用正則表達式。 – 2012-07-09 16:24:02

+0

@AndrewMin:「預處理它」......你究竟想要做什麼? DOM不需要正則表達式就可以做你想做的事情嗎? – 2012-07-09 16:28:50

相關問題