Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

PHP - preg_replace - difference between online tester and code

I usually experiment preg_replace with https://www.phpliveregex.com/#tab-preg-replace and once it works there, I implement it in PHP.

The problem is that sometimes everything is ok with the online tester but using exactly the same code in PHP doesn't work.

Here is an example.

Source code:

<a name="local-4"></a><h2>La navette Entreprise</h2>
<img src="https://www.laurentwillen.be/wp-content/uploads/sites/21/2018/04/intrepid-museum-photo-7.jpg" alt="intrepid museum photo" width="2500" height="1667" class="alignleft size-full wp-image-2407" />
<div class="wrapwhite">
La navette Entreprise est entreposée à l’arrière du porte-avion dans un bâtiment couvert aménagé spécialement pour l’occasion. L’accès au hangar est compris dans le prix du billet d’entrée. La navette est dans un état impeccable, j’ai été surpris par la taille de l’engin, j’imaginais la navette plus petite, elle occupe d’ailleurs presque tout l’espace dans son hangar qui semble être trop petit pour elle. Les visiteurs n’ont donc pas beaucoup de recul pour se rendre compte de ses dimensions ou pour prendre une photo de la navette dans son intégralité.
</div>
<div class="content-jumper">&nbsp;</div>

Then I use this:

preg_replace('/<img(.*)>/', 'aaa', $content);

Online I get the expected result which is replacing all img tags by aaa. When I try this in PHP, it doesn't work at all. It fails to find a match and no replacement is done.

I'm probably missing something simple here, it's not the first time I'm facing this issue but I can't figure out directly what the issue is.

My ultimate goal would be to replace all img tags in the page to use amp-img tags for AMP but I have many different types of IMG (with sometimes alt, sometimes not, sometimes with src to start, sometimes not,... so I need to find a generic catch all thing to avoid using regex for each case)

Do you have any idea?

Comments