This question already has an answer here:
- RegEx match open tags except XHTML self-contained tags 34 answers
- How to convert HTML to BBCode 2 answers
I have a form which takes HTML, converts it to BBCode and stores it in the database.
Most tag renaming is easily handled with a simple switch, however, text alignment is causing me some trouble.
HTML is <div style="text-align: right;"></div>
and I need to convert to BBcode [right][/right]
So I can easily do str_replace on the opening tag but for the closing tag I am replacing but that could be [/left],[/center] or [/right]. I need to know what the opening tag is before I can set it, hence the problem.
I am bad at simple regex so this one is even more difficult.
In logic terms I am trying to do this:
$str = str_replace("</div>","$align_value",$str);
But I need to know what the opening tag is to set the correct closing tag.
The expected result is it will check what the opening tag is:
if($opening_tag = '<div style="text-align: right;">')
{
$closing_tag = '[/right]';
} else if($opening_tag = '<div style="text-align: center;">')
{
$closing_tag = '[/center]';
} else if($opening_tag = '<div style="text-align: left;">')
{
$closing_tag = '[/left]';
} else {
// Some other div that isn't aligned so do nothing
}
But they key is being able to find what the opening tag is first. Any help appreciated
</div>