2010-12-14 62 views
0

我试图从[attname src =“http://example.org”]提取src属性somecontent [attname src =“http://www.example.com” ]正则表达式 - PHP中的分隔符问题

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

但是它不工作 - 有后第二"没有停止,什么样的结果在:attname src="http://example.org"] somecontent [attname src="http://www.example.com

回答

1
preg_match_all('#attname src="([^"]*)"#', $buffer, $bufferarr); 
+0

谢谢,它工作得很好。 – Paul 2010-12-14 20:28:40

0

不是最好的解决方案,但不管怎么说它完成工作:

$str = '[attname src="http://example.org"] somecontent [attname src="http://www.example.com"]'; 
preg_match_all('/attname src=\"(.*?)\"/', $str, $match); 
var_dump($match); 
2

默认情况下,+*是“贪婪” - 他们吞噬尽可能多的字符,因为他们可以。这就是为什么你得到超过你想要的。如果您向他们添加?+?*?),他们将会非贪婪,并会尽快停止。

您正则表达式也看起来不对。它应该是像#attname src="[^"]*?"#

+0

有用 - 谢谢! – Paul 2010-12-14 20:38:42