字符转义有关问题

WBOY
Release: 2016-06-13 13:27:32
Original
1123 people have browsed it

字符转义问题
比如这样的字符串
[td]For God\'s sake! [/td]
我把单引号转义了,存进数据库。

显示文章的时候,发现文章都带有转义斜杠了。
而不转义直接保存进数据库,有些时候插入数据库会错误。这该怎么办呀?


//转换某些特殊字符
function format_html($str){
$str = str_replace('“', '“',$str);
$str = str_replace('”', '”',$str);
$str = str_replace('·', '·',$str);
$str = str_replace('‘', '‘',$str);
$str = str_replace('’', '’',$str);
$str = str_replace('…', '…', $str);
$str = str_replace('—;', '―', $str);
return $str;
}

另外,php有内置函数的作用能达到我上面那个函数一样的效果吗?因为上面转的也不全完整。

------解决方案--------------------

PHP code
$str = "For God's sake!";
//入库时
...INSERT INTO tb ... col = '".addslashes($str)."'...
//输出时
echo htmlspecialchars($str);
<br><font color="#e78608">------解决方案--------------------</font><br>mysql_real_escape_string
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

mysql_real_escape_string
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template