2015-07-20 76 views
0

對於某種原因這個(凌亂的東西)不工作..它是混亂的,因爲我改變了很多,找出哪些是錯的..的preg_match不正常工作

問題:的preg_match( 「/\"h4(.*?)/h4/s」,$ theMAGICresult,$ output_game);

我知道這場比賽的工作。(normaly);我沒有檢查 http://www.phpliveregex.com/我做了一個。 foreach循環,而循環.. 我嘗試preg_match_all ..我提供一個正確的圖案,主題正確的字符串 ,我把它放在一個正確的數組,並獲取它,但它是 搶先y ..找到答案後2小時我放棄了..你能幫我 ??

preg_match_all("/<li class=\"completed\">(.*?)<\/li>/s", $all, $output); 
/* $all contains something like  xx <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li>xxxxxxxxxxxx */ 
$arrayLoop = array(); //makes sure it's an array 
$arrayLoop = $output[0]; // makes $output[0] the default array 

//$arrayLoop[0] is now <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li> 
$i = 1; //while loop because, had first foreach loop etc.. 
while($i < count($arrayLoop)) { 
$theMAGICresult = $arrayLoop[$i]; // to be sure $arrayLoop[$i] is valid 

//theMAGICresult is now : <li class="completed">xx"h4GOLDFISH/h4xxxxxx</li> 
preg_match("/\"h4(.*?)\/h4/s", $theMAGICresult, $output_game); 

echo $output_game[1]; //is nothing while is should have GOLDFISH 

    $i++; 
} 
print_r($output_game); //empty array 
+0

如果我猜的話,我會說你試圖解析一些HTML。你嘗試了一個正則表達式,但被卡住了,現在你試圖強制你的感知解決方案。這被稱爲[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。還有一個關於[爲什麼不能用正則表達式解析HTML]的極好的解釋(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)。你嘗試過PHP的[DOMDocument](http://php.net/manual/en/class.domdocument.php)嗎?如果沒有,你應該。 –

+0

[Works for me](http://3v4l.org/3BmlK)。 – Siguza

+0

@ N.B。感謝但我處理的HTML作爲一個字符串... //串(722) 我甚至刪除「< & >」從H4,以確保它是一個字符串.. –

回答

2

嘗試:

preg_match("/\\"h4(.*?)\/h4/s", $input_line, $output_array); 

固定例如:

$output_game = []; 
$theMAGICresult = '<li class="completed">xx"h4GOLDFISH/h4xxxxxx</li>'; 
preg_match('/\"h4(.*?)\/h4/s', $theMAGICresult, $output_game); 
var_dump($output_game);