I'm trying to clean some pages from my blog and modify the images tags by using preg_replace. When an image has been cleaned, I add the data attribute data-updated to avoid modifying them a second time.
$final = preg_replace('/<img(.*?)>/', '<img$1 data-updated=\'1\'>', $final);
But the next time I run the cleaning, the data-updated attribute is added a second time. I could do a str_replace to remove the additional data-updated but I'd like to avoid adding it through a regex in the first place.
i have tried using [^data-updated] with no success and I have found a similar post here: preg_replace expression can't include string but replacing data-fancy by data-updated doesn't work
Is there a way to only add data-updated if it's not already there? There are many other tags in the so I need to be able to check the presence of data-updated anywhere in the img tag
Here is an example of such an image:
<img srcset="xxx" src="yyy" loading="lazy" data-style="aspect-ratio:4/3;" data-placeholder="4-3" data-updated="y" alt="" width="100%">
Thanks! Laurent
[^data-updated]negates a character class, here specifically not matchingadeptu-. You can't use that syntax for negating strings. If you do want to use regex, you'd want to use a negative lookahead instead.