Skip to content
Advertisement

Get attribute value and replace tag PHP

Consider the following HTML…

JavaScript

And this array…

JavaScript

How would I replace the var tags where I am able to use the data-mach attribute as the key for replacing the data?

The end result would be…

Hello Elon, your email is elon@spacex.com

I know I can do…

JavaScript

But how do I make the “replaced” value use the “data-mach” attribute?

Advertisement

Answer

Regex is not optimized, but you could try with https://www.php.net/manual/ru/function.preg-replace-callback.php

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement