最近在写bbs中,遇上代码转换问题。寻找了很久,才得到一个比较完善的解决办法,可以彻底还原发文者的原文。
以下贴出,供大家指正。
系统:linux php4 oracle8i
<?php //--标题,名字等字段入库处理(去首尾空格) 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) , "<br>", $string); $string = ereg_replace(chr(32) , " ", $string); return $string; } //--显示在WEB页面,不过滤HTML代码; function trans_web($string) { $string = ereg_replace(chr(10) , "<br>", $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) , "<br>", $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代码。
本文地址:
转载随意,但请附上文章地址:-)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
