如何正确地转义数据以供浏览器解析?用于浏览器的addslahes()


How to properly escape data for the browser to parse? addslahes() for the browser?

我一直在使用addslahes()。但这会转义数据库字符。然而,我需要在下面的环境中转义HTML字符:

或者更好的想法是,我计划不使用document.write(),只使用.innerHTML。如果我不给窗户写信,我会需要逃跑吗?

目前我使用document.write(),如果我去掉addslashes()。。然后,当浏览器在关闭输入字符串时读取下一个撇号时,代码中断。

是否有类似于addslashes()的功能,但用于浏览器数据而不是数据库数据?

PHP生成(页面内部:

        <?php 
            $Object = new ObjectMaker();
            $ObjectTweet=$Object->makeTweetSmall();
            $ObjectTweet->pageInsert(); 
        ?>

称为的函数

public function pageInsert()
{
    $resultArray=$this->DatabaseObject->_pdoQuery('multiple', 'tweet_model');
    $resultAml = MarkTweet::up($resultArray);
    $resultAml = addslashes($resultAml); // this is a hack that does not work / do it correctly 
    $embeddedAml = "<script type='text/javascript'>document.write(Arc.ViewHTweet('" . $resultAml . "'))</script>";
    Control::send($embeddedAml);
}

HTML/嵌入式Javascript输出

<script type='text/javascript'>document.write(Arc.ViewHTweet(
    '1|Test Account|1329782549|1329782546|''''||1|Test     
    Account|1329782549|1329782544|''''||1|Test 
    Account|1329782549|1329782540|hi||1|Test     
    Account|1329782549|1329781792|''''||1|Test 
    Account|1329782549|1329781707|hi''''||1|Test      
    Account|1329782549|1329781701|/||1|Test 
    Account|1329782549|1329781675|<a class=''bookmark_tweet'' target=''_blank'' href=''http://bing.com''>hi''''</a>'
))</script>

Addslashes仅对DB查询有用。对于浏览器输出,您需要编码:

PHP的htmlspecialchar():

http://ca3.php.net/manual/en/function.htmlspecialchars.php