php preg_match_all 说明

php preg_match_all explanation

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

谁能解释一下这个例子中函数的每个部分在做什么?

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

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

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

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