Hello.
I want to try and make a BBCode parser, and as far as I know, preg functions are the most convenient to find and replace bbcode with html.
In the past I have used a lot of str_replace over and over, which isn't very efficient.
I want to create a BBCode and store it in the DB, so given the BBCode structure:
[image]{URL}[/image]
and the HTML: <img src="{URL}" alt="" .. />
How can I use PHP to compare these two inputs and find the necessary preg pattern to use when parsing the BBcode in content?
What I want to achieve is the DB also storing this pattern, and when it is time to parse a large text for bbcodes, it will know exactly how to do it. No hardcoding.
I searched a bit to try and find relevant topics on this, but I could only find exact patterns on how to resolve url bbcode etc, so pardon me if this really does exist somewhere.
Store the regexp and replacement patterns in the DB:
regExp: \[image\](.*?)\[/image\]
replacement: <img src="$1"/>
Then later retrieve these from the database and call preg_replace()
with them as arguments. Note that you should be able to do it with a single call, since you can specify the regexp and replacement as arrays, and it will perform all of them at once.