Home > Backend Development > PHP Tutorial > 转义符的一点总结_PHP

转义符的一点总结_PHP

WBOY
Release: 2016-06-01 12:32:35
Original
1244 people have browsed it

最近在写bbs中,遇上代码转换问题。寻找了很久,才得到一个比较完善的解决办法,可以彻底还原发文者的原文。
以下贴出,供大家指正。
系统:linux+php4+oracle8i


//--标题,名字等字段入库处理(去首尾空格)
function trans_string_trim($str) {
$str=trim($str);
$str=eregi_replace("'","''",$str);
$str=stripslashes($str);
return $str;
}

//--文章入库处理,即textarea字段;
function trans_string($str) {
$str=eregi_replace("'","''",$str);
$str=stripslashes($str);
return $str;
}

//--从库中显示在表单中;在text中以trans转换,在textarea中,无需转换,直接显示

//--显示在WEB页面,过滤HTML代码;包括链接地址
function trans($string) {
$string=htmlspecialchars($string);
$string=ereg_replace(chr(10),"
",$string);
$string=ereg_replace(chr(32)," ",$string);
return $string;
}

//--显示在WEB页面,不过滤HTML代码;
function trans_web($string) {
$string=ereg_replace(chr(10),"
",$string);
$string=ereg_replace(chr(32)," ",$string);
return $string;
}

//--显示在WEB页面,过滤HTML代码及头尾空格,主要用于显示用户昵称
function trans_trim($string) {
$string=trim($string);
$string=htmlspecialchars($string);
$string=ereg_replace(chr(10),"
",$string);
$string=ereg_replace(chr(32)," ",$string);
return $string;
}

//--显示在span中;
function trans_span($string) {
$string=ereg_replace(chr(10),"n",$string);
$string=ereg_replace(chr(32)," ",$string);
$string=ereg_replace('"',""",$string);
return $string;
}

//--在WEB上显示cookie,过滤html
function trans_cookie($str) {
$str=trans($str);
$str=stripslashes($str);
$str=eregi_replace("''","'",$str);
return $str;
}
?>
---------------------------
最后,顺带补充一点,如果在span中显示文章中的一段,采用substr取定长字符串时,记得在span的参数后面多加一个空格,否则遇上截到半个汉字时,会搞乱html代码。

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