Jump to content

Recommended Posts

Here is some code that converts many different BBCode tags. Take out whatever you don't want to use:

 

<?php

//bbcode

$patterns = array(

 //BB Code
 "/\[([bisu]|marquee)\](.*?)\[\/\\1\]/i",
 "/\[url\](.*?)\[\/url\]/i",
 "/\[url=(.*?)\](.*?)\[\/url\]/i",
 "/\[img\](.*?)\[\/img\]/i",
 "/\[quote\](.*?)\[\/quote\]/i",
 "/\[code\](.*?)\[\/code\]/i",
 "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/i",
 "/\[marquee\](.*?)\[\/marquee\]/i",
 "/\[br\]/i",

 //Emoticons
 "/\:\)/",
 "/\:\(/",
 "/\:O/",
 "/\:P/",
 "/\:\|/",
 "/\:D/",
 "/\:\?/",
 "/\;\)/");

$replacements = array(

 //BB Code
 "<\\1>\\2</\\1>",
 "<a href=\"\\1\">\\1</a>",
 "<a href=\"\\1\" target=\"_blank\">\\2</a>",
 "<img border=\"0\" src=\"\\1\">",
 "<div><b>Quote:</b> <i>\\1</i></div>",
 "<b>Code:</b><div style=\"line-height:12px; width:99%; white-space:nowrap; overflow:auto; max-height:25em;\">\\1</div>",
 "<font \\1=\"\\2\">\\3</font>",
 "<br />",

 //Emoticons
 "<img src=\"smilies/happy.gif\" border=\"0\">",
 "<img src=\"smilies/angry.gif\" border=\"0\">",
 "<img src=\"smilies/omg.gif\" border=\"0\">",
 "<img src=\"smilies/tounge.gif\" border=\"0\">",
 "<img src=\"smilies/dry.gif\" border=\"0\">",
 "<img src=\"smilies/biggrin.gif\" border=\"0\">",
 "<img src=\"smilies/confused.gif\" border=\"0\">",
 "<img src=\"smilies/wink.gif\" border=\"0\">"

);


$string = "this string has [b]bold text[/b] in it";

$result = preg_replace($patterns,$replacements,$string);

echo $result;

?>

Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505705
Share on other sites

try something like this

<?php
$array1 = array("[","]");
$array2 = array("<",">");
$string = "[b] Hello World [/b]";
echo "$string <BR>";
echo str_replace($array1,$array2,$string);
?>

 

prints out

Hello World

<b>Hello World</b>

Using paul2463's method, how do I insert an image?
Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505723
Share on other sites

Using paul2463's method, how do I insert an image?

 

What image? You asked how to transform BBCode bold tags into HTML Bold tags - so that is what paul2436 provided.

 

I made the assumption that you may need more than just the ability to handle bold tags, so I gave you a comprehensive solution - that also includes images.

 

However, I made an assumption. If you need more than bold functionality you need to state that.

Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505800
Share on other sites

OK Sorry. :)

 

How do I also create:

 

  • Lists
  • Hyperlinks

 

Did you look at the code I provided? It already has support for links. As for Lists, how do you want it to work? Do you want the user to specify each list item or do you want to interpret a link break as a new list item?

Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505872
Share on other sites

I have looked, and have come up with the following code. With reference to the list, how do I allow the user to specify each list item? Also, using my method below, how do I add hyper links? :)

 

<?php
$tags = array("&" => "&",
"<" => "<",
">" => ">",
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[img]" => "<img src='",
"[/img]" => "'>"),
"[url]" => "<a href='",
"[/url]" => "'>");
$parsedtext = str_replace(array_keys($tags), array_values($tags), $text);
echo $parsedtext;
?>

Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505915
Share on other sites

I have looked, and have come up with the following code. With reference to the list, how do I allow the user to specify each list item? Also, using my method below, how do I add hyper links? :)

 

You can't add hyperlinks to that code - you need to use preg_replace with the appropriate regualr expressions - which I already provided. This doesn't need to be difficult, just use the code I provided. I have added comments to the code so you can see what is supported.

 

<?php

//bbcode

$patterns = array(

  //BB Code
  "/\[([bisu]|marquee)\](.*?)\[\/\\1\]/i", //Handles bold, italic, strikethrough & underline
  "/\[url\](.*?)\[\/url\]/i", //Handles URLs w/o link description
  "/\[url=(.*?)\](.*?)\[\/url\]/i", //Handles URLs w/ link description
  "/\[img\](.*?)\[\/img\]/i", //handles images
  "/\[quote\](.*?)\[\/quote\]/i", //handles quote blocks
  "/\[code\](.*?)\[\/code\]/i", //Handles code blocks
  "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/i", //Handles font sizing
  "/\[br\]/i", //Handles line breaks

$replacements = array(

  //BB Code
  "<\\1>\\2</\\1>", //Handles bold, italic, strikethrough & underline
  "<a href=\"\\1\">\\1</a>", //Handles URLs w/o link description
  "<a href=\"\\1\" target=\"_blank\">\\2</a>", //Handles URLs w/ link description
  "<img border=\"0\" src=\"\\1\">", //handles images
  "<div><b>Quote:</b> <i>\\1</i></div>", //handles quote blocks
  "<b>Code:</b><div style=\"line-height:12px; width:99%; white-space:nowrap; overflow:auto; max-height:25em;\">\\1</div>", //Handles code blocks
  "<font \\1=\"\\2\">\\3</font>", //Handles font sizing
  "<br />", //Handles line breaks

);


$string = "this string has [b]bold text[/b] in it";

$result = preg_replace($patterns,$replacements,$string);

echo $result;

?>

 

For the lists you need to show how you want the user to enter the tags.

 

Like this

[list]Item 1
Item 2
Item 3[/list]

 

Or this

[list]
[*]Item 1
[*]Item 2
[*]Item 3[/list]

 

Or what???

Link to comment
https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505928
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.