Search code examples
phpjqueryreplacenl2br

Dealing with NL2BR from Database to HTML with Javascript


I am having difficulty with displaying HTML it seems. haha, let me explain.

I have 1 template file for "comments"... and it tells things where to go and such in the html. When Adding, Updating and Selecting any of the "comments"

IE:

<div class='comment'>
   <div>{$name}</div>
   <div>{$comment}</div>
</div>

So within my comment I need to pull the COMMENT from the database which includes, \n

So I go like this.

$comment = nl2br($comment);
<div class='comment'>
   <div>{$name}</div>
   <div>{$comment}</div>
</div>

And this does work... But when I do an UPDATE via jQuery I use,

$("#"+ target +"").replaceWith(responseText);

And the responseText includes all HTML... but some reason, it still is including the \n... and not

I don't know if this is a limitation with Javascript, or rendering issues. Just not sure where else to go here...Any thoughts?


Solution

  • Well this was a tad strange, there was some issues that I didn't fully test and sorry for maybe not clarifying. But mysql_real_escape_string() was causing issues with the \n being stored in the database.

    There for I am looking at using this function instead. Found on php.net's website

    function mysql_escape_mimic($value) {
            if(isset($value)) 
            {
                if(is_array($value)) {
                    return array_map(__METHOD__, $value);
                }
    
                if(!empty($value) && is_string($value)) {
                    //return str_replace( array('\\',   "\0",  "\n",  "\r",   "'",  '"',  "\x1a"), 
                    //                  array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $value);
    
                    return str_replace( array('\\',   "\0",  "\r",   "'",  '"',  "\x1a"), 
                                        array('\\\\', '\\0', '\\r', "\\'", '\\"', '\\Z'), $value);
                }
    
                return $value;
            }
        }