0

[attname src="http://example.org"] somecontent [attname src="http://www.example.com"] から src 属性を抽出しようとしています。

私が今持っているもの:
preg_match_all('#attname src=".*[^"]#', $buffer, $bufferarr);

しかし、それは機能しません - second の後に停止がないため、次"のようになります。attname src="http://example.org"] somecontent [attname src="http://www.example.com

4

3 に答える 3

2

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="[^"]*?"#.

于 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 に答える