Jump to content

[SOLVED] HTML tags...


Aureole

Recommended Posts

Say if I wanted to allow people to use <strong> <em> <a href etc. when posting a comment on my story would PHP know that it's HTML and parse it or would I need to tell it somehow that there may be HTML coming in from the form before it stores it in the database? And when I query the database do I need to let it know it might be returning some raw HTML?

Link to comment
https://forums.phpfreaks.com/topic/62185-solved-html-tags/
Share on other sites

What do I need to do in order for it to parse the HTML?

 

As far as my knowledge goes would I use strip_tags() on the input from the form before sending to the DB then when querying the DB to show it would I use strip_slashes()?

 

I think it's something like that but I'm not exactly sure on how to do it.  :-\

Link to comment
https://forums.phpfreaks.com/topic/62185-solved-html-tags/#findComment-309528
Share on other sites

<?php
function bbcode_format ($str)
    {
$str = html_entities($str);

    $simple_search = array(
                           '/\[b\](.*?)\[\/b\]/is',                               
                           '/\[i\](.*?)\[\/i\]/is',                               
                           '/\[u\](.*?)\[\/u\]/is',
	           '/\[url\=(.*?)\](.*?)\[\/url\]/is'
                          );

    $simple_replace = array(
                            '<strong>$1</strong>',
                            '<em>$1</em>',
                            '<u>$1</u>',
		    '<a href="$1">$2</a>'
                           );

    $str = preg_replace ($simple_search, $simple_replace, $str);

return $str;
  }
?>

 

I have this at the top of my file but I'm guessing I need to add something when echoing the output from the database, but I can't work it out...

 

I already have...

 

<?php
	echo nl2br("{$row['comment']}");
?>

 

So I don't know how to add it to that...

Link to comment
https://forums.phpfreaks.com/topic/62185-solved-html-tags/#findComment-309537
Share on other sites

Here is what you could do. It should work:

<?php
function parse_bbcode($t)
{
// line breaks
$t = nl2br($t);

// Basic formatting
$t = preg_replace("`\[b\](.*)\[/b\]`sUi","<b>\\1</b>",$t);
$t = preg_replace("`\[i\](.*)\[/i\]`sUi","<i>\\1</i>",$t);
$t = preg_replace("`\[u\](.*)\[/u\]`sUi","<u>\\1</u>",$t);

// URLs
$t = preg_replace("`\[url\]([\w]*[:\/\/]*[\w\.\?\/&=\:;, \-@%\?]+)\[/url\]`sUi","<a href='\\1'>\\1</a>",$t);
$t = preg_replace("`\[url\=([\w]*[:\/\/]*[\w\.\?\/&=\:;, \-@%\?]+)\](.*)\[/url\]`sUi","<a href='\\1'>\\2</a>",$t);

// Images
$t = preg_replace("`\[img\]([\w]*[:\/\/]*[\w\.\?\/&=\;, \-@%\?]+)\[/img\]`isU","<img src='\\1' alt='User posted image' />",$t);

return $t;
}

$text = <<<EOF
[b]bold[/b] [i]italic[/i] [u]underline[/u]

[url]http://google.com[/url] [url=http://google.com]Google[/url]

[img=http://www.google.com/intl/en_ALL/images/logo.gif]
EOF;

echo parse_bbcode($text);
?>

Link to comment
https://forums.phpfreaks.com/topic/62185-solved-html-tags/#findComment-309540
Share on other sites

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.