Regular expression - delimiter problem in PHP
I'm trying to extract src attributes from: [attname src="http://example.org"] somecontent [attname src="http://www.example.com"]
What I have now:
preg_match_all('#attname src=".*[^"]#', $buffer, $bufferarr);
However it doesn't work - there's no stop after second "
, what r开发者_JAVA百科esults in: attname src="http://example.org"] somecontent [attname src="http://www.example.com
By default, +
and *
are "greedy" - they gobble up as many characters as they can. That's why you get more than you want. If you add ?
to them (+?
and *?
) they will be non-greedy and will stop as soon as they can.
You regexp also looks wrong. It should be something like #attname src="[^"]*?"#
.
preg_match_all('#attname src="([^"]*)"#', $buffer, $bufferarr);
Not the best solution but anyways it get's the job done :
$str = '[attname src="http://example.org"] somecontent [attname src="http://www.example.com"]';
preg_match_all('/attname src=\"(.*?)\"/', $str, $match);
var_dump($match);
精彩评论