php preg_match_all解释

时间:2015-10-29 22:59:26

标签: php preg-match-all

我是php的新手,并且对preg_match_all()函数感到困惑。

有人可以解释一下这个例子中函数的每个部分是做什么的吗?

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

这是一个正则表达式,在你习惯它们之前它们是棘手的混蛋。它们不仅适用于PHP,每种语言都具有使用它们的功能。

这是在搜索$buffer,在<title>元素中查找<item>元素。它在<title>元素内查找由-分隔的两个文本块(第二个块是可选的。)找到的文本块保存到$titlematches中以供使用脚本。

正如在另一个答案中所提到的,http://regex101.com/是检查语法的好资源,但可能不适合初学者!