문제

I search for custom HTML tags with:

$match = preg_replace_callback("/<tag>(.*)<tag>/", function ($key) {
    $result = getTxt($key[1]);

    return $result;
}, $buffer);

It works when input is:

Abc <tag>1<tag> efg

But why it returns null on:

Abc <tag>2<tag> ef <tag>3<tag> h

I've tried to group nongreedy ending tag: /$tag(.*)($tag?)/ with same result.

도움이 되었습니까?

해결책

You need to make the .* non greedy via .*?

/<tag>(.*?)<tag>/

Say $tag is <tag>, that means $tag? becomes <tag>? which captures <tag and <tag>

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top