2015-10-29 100 views
1

我是新來的PHP和困惑的preg_match_all()函數。php preg_match_all的解釋

有人可以解釋什麼功能的每個部分在這個例子中做?

preg_match_all("/<item><title>([^<]*) - ([^<]*?)<\/title>/i", $buffer, $titlematches); 
+0

http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean –

回答

0

這是一個正則表達式,它們是棘手的混蛋,直到你習慣生活在其中。它們不僅適用於PHP,每種語言都具有使用它們的功能。

這是搜索$buffer,在<item>元素內尋找<title>元素。它正在尋找<title>元素中的兩個由-分隔的文本塊(第二個塊是可選的)。找到的文本塊保存到$titlematches以供在腳本中使用。

正如在其他答案中提到的,http://regex101.com/是一個很好的資源來檢查你的語法,但可能不適合初學者!

+1

有隱藏的html標籤,請再次檢查問題。我添加了代碼格式。 –

0
/([^<]) - ([^<]?)<\/title>/i 
1st Capturing group ([^<]) 
[^<] match a single character not present in the list below 
< a single character in the list < literally (case insensitive) 
- matches the characters - literally 
2nd Capturing group ([^<]?) 
[^<]? match a single character not present in the list below 
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy] 
< a single character in the list < literally (case insensitive) 
< matches the characters < literally 
\/ matches the character/literally 
title> matches the characters title> literally (case insensitive) 
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) 

https://regex101.com/

+0

有隱藏的html標籤,請再次檢查問題。我添加了代碼格式。 –