好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php过滤字符串函数 - php函数

php过滤字符串函数

addslashes(); stripslashes(); //对数据库教程操作时,转义特殊字符.

定义: addslashes() 函数在指定的预定义字符前添加反斜杠.

语法: addslashes(string)

注释: 默认情况下,php 指令 magic_quotes_gpc 为 on,对所有的 get、post 和 cookie 数据自动运行 addslashes(),不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义,遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测,代码如下:

<?php  function  addslashes_str( $str ){  $str = addslashes ( $str );  $str = str_replace ( $str , ";" , ';' );  return   $str ;  } //开源软件:phpfensi测试数据   function  stripslashes_str( $str ){  $str = stripslashes ( $str );  $str = str_replace ( $str , ';' , ";" );  return   $str ;  }  ?>    chop ();  //除去字符串右边空格    trim();  //除去字符串中所有空格    ltrim();  //除去字符串左边空格      htmlspecialchars();  //转换'$','"','<','>'为相应的html实体    htmlentities();  //转换所有html标记为相应的html实体      array   explode (string separator, string str);  //分割字符串    string implode(string separator,  array  arr);  //连接字符串     strtoupper ();  strtolower ();  //转换大小写    ucfirst();  //只转换第一个字符为大写     ucwords();  //转换每个words的第一个字母为大写  

iconv()

php内码转换函数,同上,因为iconv()在转换gb2312时的bug,所以要这样处理:

iconv( "utf-8", "gb2312//ignore" , $str)

mb_convert_encoding()

php的内码转换函数,版本(php 4 >= 4.0.6,php 5)

这个函数可以将各种编码互相转换

mb_convert_encoding($str,"gb2312", "utf-8");

mysql_real_escape_string()

定义 :函数转义 sql 语句中使用的字符串中的特殊字符

语法 :mysql_real_escape_string(string,connection)

说明 :本函数将 string 中的特殊字符转义,并考虑到连接的当前字符集,因此可以安全用于 mysql_query().

查看更多关于php过滤字符串函数 - php函数的详细内容...

  阅读:51次