문제

I am trying to find anything that conforms to

<element ... bentleysoft-widget="mywidget" ...></element>

and replace it with something else.

My actual string is:

$text = '<div id="joboftheday" bentleysoft-widget="joboftheday"></div><div id="adults" bentleysoft-widget="test"></div>';

My preg_replace expression is this:

$output = preg_replace('|<.*bentleysoft-widget="([a-z]+)"></.*>|', 'zzz $1', $text, -1);

However, what I get back is this:

zzz test

So it seems that, for some reason, it only picks up one occurrence. Any ideas will be much appreciated, I've spent over three hours reading and scratching my head!

도움이 되었습니까?

해결책

The problem is that .* is greedy. Take a look at this pattern /foo.*bar/ and the following text

foo hello bar world bar

The .* would match hello bar world because of the last bar.

To change this behaviour, you can simply add a ?, so the pattern would look like this

|<.*?bentleysoft-widget="([a-z]+)"></.*?>|

and it should work


Also since you want to capture the right closing tag, you should use a backreference here

|<(\S+?).*?bentleysoft-widget="([a-z]+)"></\\1>|

(\S+?) will get you the correct tag name (because \S matches everything but whitespaces), which can be backreferenced via \\1 (1 because it is the first group).

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