2015-10-29 63 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标签,请再次检查问题。我添加了代码格式。 –