I have a PHP variable that contains HTML. I need to check for occourances of a string, and have that string replaced with the result of a function.
The function has not yet been written, but will only require one variable passed to it which will be the ID of the product.
For instance, the website has products, I want the user to be able to type something into the wysiwyg editor like {{product=68}} and it will display a preset div container with the information for that product. In this case the product has ID 68 in the database, but it could be anything from 1 to whatever.
I thought about creating an array of product id’s and searching for the first part of the string, but felt it may be rather cumbersome, I think one of our resident reg exp genius may be able to shed some light on what i need to do.
So the question is… how do i search a string for occourances of {{product=XXX}} where XXX can be an integer higher than one, capture that number, and pass it to a function which creates the replacement string which ultimatley replaces the occourance of the string ?
Here is a regexp that matches {{product=##}} (doesn’t matter how many digits you enter):
Edit: Sorry, didn’t see you wanted it starting with 1:
If you want to capture the number, to it like this:
To give you a better understanding of preg_match_all, this is the content of
$matches:E.g.
$matcheswould look like this if you had the string ‘{{product=68}}{{product=70}}’: