I have the following string
“Enter Contarct Fields wherever you want and enclose it with {}.You can use field’s id or field’s fieldcode for example {25} or {FIELD_CONTRACTTEXT}”
I want to replace this {25} and {FIELD_CONTRACTTEXT} with their equivalent value from databse.
Say 25 is a ID so I wanted to put name of the field of which 25 is the ID of.
There are many examples of replacing curly braces but no solution is there for replacing content like this. Please help me, I have tried many things.
PS: Please keep in mind that occurrence of {} is dynamic and 25 will not be a static number and there can be multiple {} in a string.
You can use preg_match_all() to find the occurrences, use this array to find the values in the database and then str_replace to replace each value after that.
To begin with you will need to fetch all the occurrences:
The matches array will look like this:
You can then cylce through the array to find the individual elements that need to be replaced.
You can use the following to check the database and then replace values: