开发者

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:

<开发者_如何学C;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?


In the php file you are getting the comments with using jQuery try doing the following before echoing the data back

$comment=str_replace('\n\r', '<br />', $comment);
$comment=str_replace('\n', '<br />', $comment);
echo $comment;


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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜