Jump to content

[SOLVED] Safetly Letting users use tags


cooldude832

Recommended Posts

I have a field on my profile system that allows a users to put in a paragraph about themselves.  I want to allow some very basic styling here (like returns, bold, color etc) is there a way I can add BB tags to this easily or some other method (I"ve never used BB tags before so if someone could point me in the right direction that be great). 

Link to comment
https://forums.phpfreaks.com/topic/74132-solved-safetly-letting-users-use-tags/
Share on other sites

Heres a bbcode2html,

as this board uses BBcode i have added @ signs to stop the parsing, your need to remove all of them..

 


<?php

// A simple FAST parser to convert BBCode to HTML
// Trade-in more restrictive grammar for speed and simplicty
//
// Syntax Sample:
// --------------
// [@img]http://phpfreaks.com/images/deadrats.gif[@/img]
// [@url="http://phpfreaks.com"]phpfreaks[@/url]
// [@mail="webmasterphpfreaks.com"]Webmaster[@/mail]
// [@size="25"]HUGE[@/size]
// [@color="red"]RED[@/color]
// [@b]bold[@/b]
// [@i]italic[@/i]
// [@u]underline[@/u]
// [@list][*]item[*]item[*]item[@/list]
// [@code]value="123";[@/code]
// [@quote]John said yadda yadda yadda[@/quote]
//
// Usage:
// ------
// <?php include 'bb2html.php'; ?>
// <?php $htmltext = bb2html($bbtext); ?>



function bb2html($text)
{
  $bbcode = array("<", ">",
                "[@list]", "[*]", "[@/list]", 
                "[@img]", "[@/img]", 
                "[@b]", "[@/b]", 
                "[@u]", "[@/u]", 
                "[@i]", "[@/i]",
                '[@color="', "[@/color]",
                "[@size=\"", "[@/size]",
                '[@url="', "[@/url]",
                "[@mail=\"", "[@/mail]",
                "[@code]", "[@/code]",
                "[@quote]", "[@/quote]",
                '"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<code>", "</code>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  $newtext = nl2br($newtext);//second pass
  return $newtext;
}
?>

as this board uses BBcode i have added @ signs to stop the parsing, your need to remove all of them..

 

I don't think it will parse the BBcode between [.code][./code] tags. So no need to worry about that in the future :)

 

Just to test...

[b]test[/b]

lets see


<?php

// A simple FAST parser to convert BBCode to HTML
// Trade-in more restrictive grammar for speed and simplicty
//
// Syntax Sample:
// --------------
// [img=http://phpfreaks.com/images/deadrats.gif]
// [url="http://phpfreaks.com"]phpfreaks[/url]
// [mail="webmasterphpfreaks.com"]Webmaster[/mail]
// [size="25"]HUGE[/size]
// [color="red"]RED[/color]
// [b]bold[/b]
// [i]italic[/i]
// [u]underline[/u]
// [list][*]item[*]item[*]item[/list]
// [@code]value="123";[@/code]
// [@quote]John said yadda yadda yadda[@/quote]
//
// Usage:
// ------
// <?php include 'bb2html.php'; ?>
// <?php $htmltext = bb2html($bbtext); ?>



function bb2html($text)
{
  $bbcode = array("<", ">",
                "[list]", "[*]", "[/list]", 
                "[img=", "]", 
                "[b]", "[/b]", 
                "[u]", "[/u]", 
                "[i]", "[/i]",
                '[color="', "[/color]",
                "[size=\"", "[/size]",
                '[url="', "[/url]",
                "[mail=\"", "[/mail]",
                "[@code]", "[/@code]",
                "[@quote]", "[/@quote]",
                '"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<code>", "</code>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  $newtext = nl2br($newtext);//second pass
  return $newtext;
}
?>

 

got a feeling that the code and quote will mess it up...

 

EDIT: Holy Hell, remove @ from quote and code

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.