Jump to content

Htmlentities


3raser

Recommended Posts

Highly annoying.

 

Data is submitted to the database wrapped in the nl2br() function. When viewing the data, htmlentities is used to keep away any XSS or HTML vulnerabilities. But all new lines show up as <br /> instead of a new line. I even made a function to stop them from happening:

 

public function br2nl($string)
    {
        return str_replace('<br />', "\n", $string);
    }

 

Which doesn't work. Any advice?

Link to comment
https://forums.phpfreaks.com/topic/264417-htmlentities/
Share on other sites

Try printing out your string before br2nl() and after br2nl(), to see what is going wrong.

 

No differences from what I can tell. It remains:

 

fdsfdsfdsfdsf<br /> <br /> <br /> <br /> <br /> <br /> <br /> fdsfdsfdsfds<br /> <br /> <br /> <br /> <br /> <br /> dfdsfdsfdsf<br /> <br /> <b>wut</b>

Link to comment
https://forums.phpfreaks.com/topic/264417-htmlentities/#findComment-1355057
Share on other sites

Can you post a short script demonstrating that it doesn't work?  Something like this:

 

<?php
$str = "<br />";
function br2nl($string)
    {
        return str_replace('<br />', "\n", $string);
    }
print "Before: " . urlencode($str) . "\n";
$str = br2nl($str);
print "After: " . urlencode($str) . "\n";
?>

 

The urlencode() is there so you can see any hidden characters.

Link to comment
https://forums.phpfreaks.com/topic/264417-htmlentities/#findComment-1355058
Share on other sites

Can you post a short script demonstrating that it doesn't work?  Something like this:

 

<?php
$str = "<br />";
function br2nl($string)
    {
        return str_replace('<br />', "\n", $string);
    }
print "Before: " . urlencode($str) . "\n";
$str = br2nl($str);
print "After: " . urlencode($str) . "\n";
?>

 

The urlencode() is there so you can see any hidden characters.

 

Using your test:

 

Before: %3Cbr+%2F%3E After: %0A

 

Like I said, when using the br2nl function - nothing seems to change. The <br /> stays the same.

 

<?php
require('../structure/base.php');
$base = new base();

if(!isset($_POST['derp']))
{
    ?>

<form action="test.php" method="POST">
<textarea cols="55" rows="30" name="derp"></textarea><br/><input type="submit" value="Derp"> 
</form>

    <?php
}
else
{
    $content = nl2br($_POST['derp']);
    
    echo $base->br2nl(htmlentities($content)).'<br/><hr><br/>'.$content;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/264417-htmlentities/#findComment-1355062
Share on other sites

By running it through htmlentities you are converting your <br /> tags to <br /> so that is what you need to search for and replace.

 

public function br2nl($string)
    {
        return str_replace('<br />', "\n", $string);
    }

 

Eh, gets rid of the <br /> - But no new lines. I'm assuming I need to use something other than \n?

Link to comment
https://forums.phpfreaks.com/topic/264417-htmlentities/#findComment-1355079
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.