Forum Moderators: coopster

Message Too Old, No Replies

HTML to BBCode help

preg_replace

         

myacidpacemaker

12:30 pm on Jan 3, 2009 (gmt 0)

10+ Year Member



Hi,

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);

eelixduppy

2:40 pm on Jan 3, 2009 (gmt 0)



Maybe try something like this. I cleaned it up a bit.

[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]

myacidpacemaker

1:07 am on Jan 4, 2009 (gmt 0)

10+ Year Member



Hi eelixduppy,

Thanks for the tidy up & quick reply.

Unfortunately thats the code im using to replace bbcode -> html to store in my db.

im wanting to reverse it for an edit page so that the html is replaced by bbcode.

thanks

dreamcatcher

7:33 pm on Jan 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don`t you just store the BBCode in raw format in the database and parse it when it comes from the database? Rather than converting the HTML when it goes in?

dc

myacidpacemaker

11:49 pm on Jan 4, 2009 (gmt 0)

10+ Year Member


Hi Dreamcatcher,

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]

eelixduppy

1:18 am on Jan 5, 2009 (gmt 0)



Not bad. As a quick tip, str_replace takes arrays as search and replace parameters also. This could be used to your advantage in this case here as follows:

[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. ;)

myacidpacemaker

4:03 am on Jan 5, 2009 (gmt 0)

10+ Year Member



nice. thanks for that