0

我正在尝试从以下位置提取 src 属性: [attname src="http://example.org"] somecontent [attname src="http://www.example.com"]

我现在拥有的:
preg_match_all('#attname src=".*[^"]#', $buffer, $bufferarr);

但是它不起作用 - 在 second 之后没有停止",结果是:attname src="http://example.org"] somecontent [attname src="http://www.example.com

4

3 回答 3

2

默认情况下,+它们*是“贪婪的”——它们会尽可能多地吞噬角色。这就是为什么你得到的比你想要的更多。如果您添加?到它们(+?*?),它们将是非贪婪的,并且会尽快停止。

你的正则表达式也看起来不对。它应该是类似的东西#attname src="[^"]*?"#

于 2010-12-14T20:30:55.650 回答
1
preg_match_all('#attname src="([^"]*)"#', $buffer, $bufferarr);
于 2010-12-14T20:22:52.487 回答
0

不是最好的解决方案,但无论如何它都能完成工作:

$str = '[attname src="http://example.org"] somecontent [attname src="http://www.example.com"]';
preg_match_all('/attname src=\"(.*?)\"/', $str, $match);
var_dump($match);
于 2010-12-14T20:25:21.040 回答