i’m not very firm with preg_replace – in other Words i do not really understand – so i hope you can help me.
I have a string in a Text like this one: [demo category=1] and want to replace with the Content of Category (id=1) e.g. “This is the Content of my first Category“
This is my startpoint Pattern – that’s all i have:
'/[demo\s*.*?]/i';
Hope you can help?
Firstly, you need to escape the square brackets as they are special characters in PCREs:
Secondly, it sounds like you want to do something with the digit at the end, so you’ll want to capture it using parenthesis:
The braces will capture
\d+and store it in a reference.\d+will match a string of numbers only.Finally, it sounds like you need to use
preg_replace_callbackto perform a special function on the matches in order to get the string you want: