Forum Moderators: coopster
Just wondering if anyone could help me reverse this preg replace?
I've managed to replace the bbcode to html before inserting into my db but am now wanting an edit page where user will see their post back in bb code.
Any ideas?
thanks in advance
$bb_replace = array('/(\[[Bb]\])(.+)(\[\/[Bb]\])/','/(\[[Ii]\])(.+)(\[\/[Ii]\])/','/(\[[Uu]\])(.+)(\[\/[Uu]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/','/(\[url])(.+)(\[\/url\])/','/(\[img])(.+)(\[\/img\])/');
$bb_replacements = array('<b>\\2</b>','<i>\\2</i>','<u>\\2</u>',"<a href='\\2' target='_blank'>\\4</a>","<a href='\\2' target='_blank'>\\2</a>","<img src='\\2' alt='' style='border:0' />");
$post= preg_replace($bb_replace, $bb_replacements, $post);
[pre]
$bb_replace = array(
"/\[[Bb]\](.*)\[\/[Bb]\]/",
"/\[[Ii]\](.*)\[\/[Ii]\]/",
"/\[[Uu]\](.*)\[\/[Uu]\]/",
"/\[url=(.+)\](.*)\[\/url\]/",
"/\[url](.*)\[\/url\]/",
"/\[img](.*)\[\/img\]/"
);
#
$bb_replacements = array(
"<b>\\1</b>",
"<i>\\1</i>",
"<u>\\1</u>",
"<a href='\\1' target='_blank'>\\2</a>",
"<a href='\\1' target='_blank'>\\1</a>",
"<img src='\\1' alt='' style='border:0' />"
);
[/pre]
I echo the output more than I edit it so is easier to store it as html then have to replace every time I view it.
Figured it out anyways, bit ugly but does the job.
$show =str_replace("<a href='", "[url=", $show);
$show=str_replace("'target='_blank'", "", $show);
$show=str_replace("</a>", "[/url]", $show);
$show=str_replace("<img src='", "[img]", $show);
$show=str_replace("'alt=''style='border:0'/>", "[/img]", $show);
$show=str_replace("<font color='", "[color=", $show);
$show=str_replace("</font>", "[/color]", $show);
$show=str_replace("'>", "]", $show);
$show=str_replace(">", "]", $show);
$show=str_replace("<", "[", $show);
$show=str_replace("<b>", "[b]", $show);
$show=str_replace("</b>", "[/b]", $show);
$show=str_replace("<i>", "[i]", $show);
$show=str_replace("</i>", "[/i]", $show);
$show=str_replace("<u>", "[u]", $show);
$show=str_replace("</u>", "[/u]", $show);
$show=str_replace("<s>", "[s]", $show);
$show=str_replace("</s>", "[/s]", $show);
[1][[b]edited by[/b]: eelixduppy at 1:20 am (utc) on Jan. 5, 2009][/1]
[1][edit reason] fixed formatting [/edit][/1]
[pre]
$search = array(
"<a href='",
"'target='_blank'",
"</a>",
"<img src='",
etc...);
#
$replace = array(
"[url=",
"",
"",
"[img]",
etc...);
#
$show = str_replace($search, $replace, $show)
[/pre]
And sorry about before. I wasn't thinking. ;)