>백엔드 개발 >PHP 튜토리얼 >如何运用PHP转义符解决代码转换问题_PHP教程

如何运用PHP转义符解决代码转换问题_PHP教程

WBOY
WBOY원래의
2016-07-15 13:29:00942검색

在学习了系统:linux php4 oracle8i

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li>
<li class=""><span>//--标题,名字等字段入库处理(去首尾空格)  </span></li>
<li class="alt"><span>function trans_string_trim($str) {  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">trim</font></span><span>($str);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">eregi_replace</font></span><span>("'","''",$str);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">stripslashes</font></span><span>($str);  </span>
</li>
<li class="alt"><span>return $str;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//--文章入库处理,即textarea字段;  </span></li>
<li class="alt"><span>function trans_string($str) {  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">eregi_replace</font></span><span>("'","''",$str);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">stripslashes</font></span><span>($str);  </span>
</li>
<li class=""><span>return $str;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//--从库中显示在表单中;在text中以trans转换,在textarea中,无需转换,直接显示  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//--显示在WEB页面,过滤HTML代码;包括链接地址  </span></li>
<li class=""><span>function trans($string) {  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">htmlspecialchars</font></span><span>($string);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(10),"</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>",$string);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(32)," ",$string);  </span>
</li>
<li class=""><span>return $string;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//--显示在WEB页面,不过滤HTML代码;  </span></li>
<li class=""><span>function trans_web($string) {  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(10),"</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>",$string);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(32)," ",$string);  </span>
</li>
<li class="alt"><span>return $string;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//--显示在WEB页面,过滤HTML代码及头尾空格,主要用于显示用户昵称  </span></li>
<li class="alt"><span>function trans_trim($string) {  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">trim</font></span><span>($string);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">htmlspecialchars</font></span><span>($string);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(10),"</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>",$string);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(32)," ",$string);  </span>
</li>
<li class=""><span>return $string;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//--显示在span中;  </span></li>
<li class=""><span>function trans_span($string) {  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(10),"n",$string);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>(chr(32)," ",$string);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>('"',""",$string);  </span>
</li>
<li class=""><span>return $string;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//--在WEB上显示cookie,过滤html  </span></li>
<li class=""><span>function trans_cookie($str) {  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">trans</font></span><span>($str);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">stripslashes</font></span><span>($str);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">eregi_replace</font></span><span>("''","'",$str);  </span>
</li>
<li class=""><span>return $str;  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

最后,关于PHP转义符的用法再顺带补充一点,假如在span中显示文章中的一段,采用substr取定长字符串时,记得在span的参数后面多加一个空格,否则遇上截到半个汉字时,会搞乱html代码。

以上就是我们为大家奉上的关于PHP转义符解决代码转换问题的办法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446402.htmlTechArticle在学习了 系统:linux php4 oracle8i ? //--标题,名字等字段入库处理(去首尾空格) functiontrans_string_trim($str){ $ str = trim ($str); $ str = eregi_replace ("'"...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.