2017-04-14 65 views
-1

嘗試捕獲標記之間的所有文本。Php preg_match_all的行爲不如預期

代碼:

$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>'; 

$match = '/<test>(.*)<tests>/'; 

preg_match_all($match, $test, $nextLink); 

的print_r的結果:

Array ([0] => Array ([0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234) [1] => Array ([0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234)) 
+0

使用非貪婪定量'' – Barmar

+0

不結果包括所有的'',一切之間''在(*?)? – Barmar

+0

使用查看源查看它實際返回的內容,因爲瀏覽器隱藏了看起來像HTML標記的內容。 – Barmar

回答

2

您正則表達式的語法是貪婪。使用如下因素:

$match = '/<test>(.*?)<tests>/'; 
+0

謝謝!但爲什麼??對不起,與正則表達式不是最好的! –

+0

@HunterS閱讀教程,並特別關注有關貪婪的部分:http://www.regular-expressions.info/repeat.html – Barmar