FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


htmlspecialchars





DanielXP
In my site users can post tutorials and if they want to post html i had to put a html filter on

Code:
$tutorial = htmlspecialchars($_POST['tutorial']);


Now if people leave a line it will just display it as one line althought in the database it is multi line

so some how i need to replace a line with <br>

How can i do this?
BlackSkad
Code:
$tutorial = nl2br(htmlspecialchars($_POST['tutorial']));


The function nl2br converts all linefeeds (\n, \r, \n\r, etc.) to <br /> (or <br>, depending on the php-version).
The name in fact is leedspeak for "new line to <br />"
DanielXP
Thanks works great.

Your very good at PHP
Simulator
DanielXP wrote:
Thanks works great.

Your very good at PHP


Heres a script I use, it also allows BBcode:

Code:
function convert_for_html ($matches) {
                $regex[0] = "[";
                $regex[1] = "]";
                $replace[0] = "[";
                $replace[1] = "]";
                ksort($regex);
                ksort($replace);
                $treated = str_replace($regex, $replace, $matches[1]);
                $output = '<table class="blog"><tr><td>Code:</td></tr><tr><td class="blog">' . $treated . '</td></tr></table>';

                return $output;
        }


     function text($post)
     {
     //Make safe any html
$post_no_html = htmlentities($post);

//Make sure there is no whitespace at the end of the message
//It's conceivable that the user will start their message with whitespace
$post_abridged = chop($post_no_html);
//Callback function for preg_replace_callback below


        //Convert code tags
        $code_treated = preg_replace_callback(
                        "/\[code\](.+?)\[\/code\]/s",
                   "convert_for_html",
                   $post_abridged);
//Arrays for the bbCode replacements
        $bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s',
                                                1 => '/\[i\](.+?)\[\/i\]/s',
                                                2 => '/\[u\](.+?)\[\/u\]/s',
                                                3 => '/\[quote\](.+?)\[\/quote\]/s',
                                                4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s',
                                                5 => '/\[url\](.+?)\[\/url\]/s',
                                                6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s',
                                                7 => '/\[img\](.+?)\[\/img\]/s',
                                                8 => '/\[color\=(.+?)\](.+?)\[\/color\]/s',
                                                9 => '/\[size\=(.+?)\](.+?)\[\/size\]/s');

        $bbcode_replace = array(0 => '<b>$1</b>',
                                                1 => '<i>$1</i>',
                                                2 => '<u>$1</u>',
                                                3 => '<table width="80%"><tr><td>Quote:</td></tr><tr><td class="quote">$1</td></tr></table>',
                                                4 => '<table width="80%"><tr><td><strong>$1 said:</strong></td></tr><tr><td class="quote">$2</td></tr></table>',
                                                5 => '<a href="$1"><font class="blog">$1</font></a>',
                                                6 => '<a href="$1"><font class="blog">$2</font></a>',
                                                7 => '<img src="$1" alt="User submitted image" title="User submitted image"/>',
                                                8 => '<span style="color:$1">$2</span>',
                                                9 => '<span style="font-size:$1">$2</span>');

        ksort($bbcode_regex);
        ksort($bbcode_replace);

        //preg_replace to convert all remaining bbCode tags
        $post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);
      //Convert new lines to <br />
        $post_with_br = nl2br($post_bbcode_treated);
      echo $post_with_br;
     }


When you want to use it just go text($_POST["name"]); and it will print out the text
DanielXP
Don't get the br thing

how would i stop the html and do that
Stubru Freak
Like he said:

Quote:
Code:
$tutorial = nl2br(htmlspecialchars($_POST['tutorial']));


First you have
<sometext>
sometext

Then, with htmlspecialchars, it converts it to:
&lt;sometext&gt;
sometext

Then, with nl2br, it changes it to:
&lt;sometext&gt;<br />
sometext

So you should make sure you put the htmlspecialchars inside the nl2br.
DanielXP
i know that but i don't get how to do the BB (i ment BB last time but i put BR)
Related topics
Undo htmlspecialchars
htmlentities and htmlspecialchars
req: PHP to list files in directory, and link to them
PHP function(); &file=""?
error in my PHP script
need php help save command
Reducing Exploits
Something wrong with signatures
help with php needed
tweaking wordpress
Stop Scripting
phpBB 2.0.20 RELEASED
Login
Javascript onSubmit
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.