Jump to content

[SOLVED] Remove bbcode from text


jcbarr

Recommended Posts

Okay I have the following code that pulls text and subject from a phpBB database and displays it on the main page of a site. I want to pull the bbcode tags out cause it looks ugly. My question is how would I integrate that in to the code that I have below?

 

<?php
mysql_select_db ("csbl");
$query=mysql_query("SELECT post_id FROM phpbb_posts WHERE forum_id='2' ORDER BY topic_id DESC, post_id ASC LIMIT 1");
$a=mysql_fetch_array($query);
$postid=$a['post_id'];
$query=mysql_query("SELECT * FROM phpbb_posts_text WHERE post_id='$postid'");
$b=mysql_fetch_array($query);
$title=$b['post_subject'];
$text=$b['post_text'];
if (strlen($title)>38){
  echo "<font size=+1><b>";
  echo substr($title, 0, 35);
  echo "</b></font>...<br><br>";
} else {
  echo "<font size=+1><b>$title</b></font><br><br>";
}
if (strlen($text)>'225'){
  echo (substr($text, 0, 500));
  echo "...<br><div align=right><a href='http://csbl-ebl.com/forums/viewtopic.php?p=$postid#$postid'>Read More</a></div>";
} else {
  echo ($text);
}
mysql_close($dbh);

?>

 

If you aren't familiar with what bbcode looks like when you pull it out of the database as text here are some examples.

 

Bold [b:28e3b06cb2] [/b:28e3b06cb2]

Italics [i:dff082c1f7] [/i:dff082c1f7]

Underline [u:dff082c1f7] [/u:dff082c1f7]

 

The text is of course enclosed inside those tags. The id after the : changes, but before the : is always the same.

 

I'm really only worred about those three tags, as that is all that the board allows at the moment.

 

Any direction on this?

Link to comment
https://forums.phpfreaks.com/topic/41512-solved-remove-bbcode-from-text/
Share on other sites

Well, you could use a generic match to pull them all out:

<?php
$String = "Bold [b:28e3b06cb2] [/b:28e3b06cb2] Italics [i:dff082c1f7] [/i:dff082c1f7] Underline [u:dff082c1f7] [/u:dff082c1f7]";
$String = preg_replace('|\[/?[biu]\[^]]+)\]|ims', '', $String);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.