jose sanchez
jose sanchez

Reputation: 339

Preg_replace the last ocurrence starting from end

I have a code that replaces all ocurrences on loaded html, how should I modify it to replace only the first ocurrence starting from the end of loaded html ?

$body = preg_replace('#<div>https://site.com/(.*?)<br />#', '<a href="site.com/$1" target="_blank">https://site.com/$1</a><br />', $body);

My loaded html: (Is compressed, this not have any break lines)

<div class="clearfix spoiler"><div>https://site.com/?k=tZ1gXCyS!fywvS1E9rmnUJyjEWOkH0zHZ<br />https://site.com/#!BJchGYSB!Av27mwaKMv7zVNu9uL0fg3uUDfBoym2Lk9NeyZJp_1I<br />https://site.com/#!MdM3lDga!HHlk9SvherA-LYNAfDd9SBEwKUp3dn9oELCPVHf2mYc<br />https://site.com/#!EQ8CwZoA!QJVJMgn2cj-cmIX51Hn2OH8ySPYWw5Xy-e9tX53emDY<br />https://site.com/#!sRESXSSa!ZFuNpzMzTpF9DRohX4WNTDyo4pAkwuX0Gkh-vToQ5ec<br />https://site.com/#!NUtVmQbb!eT0qf05YySrq-hXHhj53lmTi1bcWsprB50NsZqh5MLg<br />https://site.com/#!IcdymLia!eoeI0xz-BAoVRnbL_1W2DQMUmBYtXQaaVS7-G69fY5w<br />https://site.com/#!QFVnFAKD!RhXtx3pPFrrpxw3kKmkw2ScEBgR1pD_byIvpWHHQ6ok<br />https://site.com/#!1VtDRI5J!ZUNHtxKTnnrF29z0AKLULSbsgLZ0-F_45TpmEhwijz8</div></div>

I need to replace the last <br />https://site.com/(.*?)</div> (links always changes) whit <div>https://site.com/$1</div> (only the last match or first match starting from the end)

So output should be like this:

<div class="clearfix spoiler"><div>https://site.com/?k=tZ1gXCyS!fywvS1E9rmnUJyjEWOkH0zHZ<br />https://site.com/#!BJchGYSB!Av27mwaKMv7zVNu9uL0fg3uUDfBoym2Lk9NeyZJp_1I<br />https://site.com/#!MdM3lDga!HHlk9SvherA-LYNAfDd9SBEwKUp3dn9oELCPVHf2mYc<br />https://site.com/#!EQ8CwZoA!QJVJMgn2cj-cmIX51Hn2OH8ySPYWw5Xy-e9tX53emDY<br />https://site.com/#!sRESXSSa!ZFuNpzMzTpF9DRohX4WNTDyo4pAkwuX0Gkh-vToQ5ec<br />https://site.com/#!NUtVmQbb!eT0qf05YySrq-hXHhj53lmTi1bcWsprB50NsZqh5MLg<br />https://site.com/#!IcdymLia!eoeI0xz-BAoVRnbL_1W2DQMUmBYtXQaaVS7-G69fY5w<br />https://site.com/#!QFVnFAKD!RhXtx3pPFrrpxw3kKmkw2ScEBgR1pD_byIvpWHHQ6ok<div>https://site.com/#!1VtDRI5J!ZUNHtxKTnnrF29z0AKLULSbsgLZ0-F_45TpmEhwijz8</div></div>

Upvotes: 1

Views: 121

Answers (2)

Evan
Evan

Reputation: 825

You can modify your search to make sure it doesn't contain <br /> for the rest of the string. In order to do so, you would input something like: #<br />https://site.com/((?:(?!<br />).)*?)</div># as your search string. The ?! is the negative lookahead assertion, which checks to make sure that the string doesn't exist ahead, and then the . captures as you would normally expect.

Upvotes: 1

Pi Marillion
Pi Marillion

Reputation: 4674

The easiest way is to add a (.*) group at the beginning and end, as well as a $ at the end, and an s modifier so that . matches newlines. The first group will contain everything up to the last instance (since .* is greedy), and the last group will catch everything afterward.

$body = preg_replace(
    '#(.*)<div>https://site.com/(.*?)<br />(.*)$#s',
    '$1<a href="site.com/$2" target="_blank">https://site.com/$2</a><br />$3',
    $body
);

Upvotes: 0

Related Questions