I need help with a project involving a BBCODE editor that can switch between a WYSIWYG editor and a code editor.
The visual editor is designed with a drag-and-drop block system for elements like pictures and text.
In the visual editor, when a user drags a new text block, the content is automatically placed between [text][/text]
tags.
In the code editor, users can freely write text without [text][/text]
tags.
To seamlessly switch between the two editors, free text needs to be enclosed in [text][/text]
tags in the code editor.
For example:
If I write text and bbcode in the code editor:
Cum haec taliaque sollicitas eius aures everberarent expositas semper eius modi
rumoribus et patentes.
[img]https://foo.com/fighters.png[/img]
Denique Antiochensis ordinis vertices sub uno elogio iussit occidi ideo efferatus,
quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia
[img]https://foo.com/fighters1.png[/img]
[img]https://foo.com/fighters2.png[/img]
Utque proeliorum periti rectores [i]primo catervas[/i] densas opponunt et fortes,
deinde leves armaturas, post iaculatores ultimasque subsidiales acies, si fors
adegerit
If I switch to the visual editor, the free text must be enclosed in [text][/text]
tags like this:
[text]Cum haec taliaque sollicitas eius aures everberarent expositas semper eius modi
rumoribus et patentes.[/text]
[img]https://foo.com/fighters.png[/img]
[text]Denique Antiochensis ordinis vertices sub uno elogio iussit occidi ideo efferatus,
quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia[/text]
[img]https://foo.com/fighters1.png[/img]
[img]https://foo.com/fighters2.png[/img]
[text]Utque proeliorum periti rectores [i]primo catervas[/i] densas opponunt et fortes,
deinde leves armaturas, post iaculatores ultimasque subsidiales acies, si fors
adegerit[/text]
I'm considering two approaches:
- Splitting text and bbcode using loops, then reconstructing the code with another set of loops.
- Using regex to extract free text and replace it accordingly.
Which method do you think is better? Is it feasible to add the tags using regex?
Thank you, Thomas