php addslashes

 更新时间:2016年11月25日 16:01  点击:2020

php addslashes

描述
字串相当于自动(字符串$海峡)
返回之前需要在数据库查询等,这些字符是单引号('),双引号引用的字符串反斜杠字符(“),反斜杠()和NUL(空字节)。

定义和用法
addslashes() 函数在指定的预定义字符前添加反斜杠。

这些预定义字符是:

单引号 (')
双引号 (")
反斜杠 ()
NULL
语法

例如一个相当于自动使用()是当您将数据输入数据库。例如,要插入到数据库中的名称赖利,您将需要逃脱。它的高度recommeneded使用数据库管理系统的具体转义函数(比如mysqli_real_escape_string()的MySQL或pg_escape_string()对PostgreSQL),但如果你使用的数据库管理系统简化版,有一个逃生的功能,以及数据库管理系统使用逃跑,你的特殊字符可以使用此功能。这只会进入数据库,额外的数据将不会被插入。具有PHP指令magic_quotes_sybase设置的意思是不是转义为'另一个'。

PHP指令magic_quotes_gpc被默认情况下,它本质上运行addslashes()作用于所有的GET,POST和COOKIE数据。不要使用addslashes()对已经被magic_quotes_gpc设置为您就做双逃跑逃脱字符串。函数get_magic_quotes_gpc()可能会派上用场检查这一点。


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


报告错误参数

海峡
该字符串转义。


报告错误返回值
返回转义字符串。

报告错误的例子

例子1的addslashes()作用的例子

<?php
$str = "Who's John Adams?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>

Who's John Adams? This is not safe in a database query.
Who's John Adams? This is safe in a database query.

下面来一个我使用递归函数。它处理多维数组

<?php
function as_array(&$arr_r)
{
 foreach ($arr_r as &$val) is_array($val) ? as_array($val):$val=addslashes($val);
 unset($val);
}

as_array($_POST);
?>

php addslashes处理$_POST $_GET数组函数

这是我的一个相当于自动版本的功能,用于处理$ _POST数组有用

<?php
function add_slashes ($an_array) {
  foreach ($an_array as $key => $value) {
    $new_array[$key] = addslashes($an_array[$key]);
  }
}
?>

then call it:

<?php add_slashes($_POST); ?>

嗨,
我使用这个递归函数。它处理多维数组。


<?php
function as_array(&$arr_r)
{
 foreach ($arr_r as &$val) is_array($val) ? as_array($val):$val=addslashes($val);
 unset($val);
}

as_array($_POST);
?>

复杂一点的。

<?php
//create array to temporarily grab variables
$input_arr = array();
//grabs the $_POST variables and adds slashes
foreach ($_POST as $key => $input_arr) {
    if(is_array($input_arr)){       
        $_POST[$key] = addslashes_array($input_arr);
    }else{
        $_POST[$key] = addslashes($input_arr);
    }
   
}

// Recursive Function to add slashes with posted array.
function addslashes_array($input_arr){
    if(is_array($input_arr)){
        $tmp = array();
        foreach ($input_arr as $key1 => $val){
            $tmp[$key1] = addslashes_array($val);
        }
        return $tmp;
    }else{
        return addslashes($input_arr);
    }
}

?>

PHP htmlspecialchars 函数

定义和用法
addslashes() 函数在指定的预定义字符前添加反斜杠。

些字符有特殊意义的HTML,并应在HTML实体的代表,如果他们要保持它们的含义。这个函数返回一个与这些转换的一些字符串的;的译本,是那些最有用的日常网络编程。如果您需要的所有HTML字符实体进行翻译,使用ヶ辆()代替。

此功能有助于防止用户提供从含有诸如在留言板或来宾簿应用HTML标记,文本。

在翻译过程为:

'&'(符号)变成'&'
'“'(双引号)变成'”'时ENT_NOQUOTES未设置。
'''(单引号)成为'''只有当ENT_QUOTES设置。
'<“(小于)变成”<'
'>“(大于)变成'>”

 

字符串
该字符串被转换。

quote_style
可选的第二个参数,quote_style,告诉函数如何处理单,双引号。默认的模式,ENT_COMPAT,是向后兼容的模式,只有99.64双引号字符和叶单引号翻译。如果ENT_QUOTES设置单一和双引号翻译,如果ENT_NOQUOTES既不是单一的,也不设置双引号的翻译。

字符集
定义中使用的字符集的转换。默认字符集是ISO - 8859 - 1。

对于这个功能的目的,标准字符集- 8859 - 1问题,ISO - 8859 - 15,UTF - 8的,cp866,cp1251,cp1252,并KOI8 - R的有效等价,如用htmlspecialchars影响的字符()占用相同这些职位的所有字符集。

以下字符集的支持在PHP 4.3.0及更高版本。

支持的字符集编码别名描述
标准- 8859 - 1 ISO8859 - 1西欧,拉美- 1
标准- 8859 - 15 ISO8859 - 15西欧,拉美9。增加了欧元符号,法国和芬兰在拉丁美洲的信失踪1字符(ISO - 8859 - 1)。
UTF - 8的ASCII兼容多字节8位Unicode。
cp866 ibm866,866 DOS的具体西里尔字符集。这字符集支持4.3.2。
cp1251的Windows - 1251,共赢1251,1251 Windows的具体西里尔字符集。这字符集支持4.3.2。
cp1252的Windows - 1252,1252的Windows西欧特定字符集。
KOI8 - R的koi8茹,koi8r俄罗斯。这字符集支持4.3.2。
繁体版950繁体中文,主要用于台湾。
简体版936简体中文,国家标准字符集。
繁体版,繁体中文字符集的扩展与香港,繁体中文。
Shift_JIS 8859,932日语
EUC - JP的EUCJP日本


注:任何其他字符集无法识别和ISO - 8859 - 1将用来代替。


double_encode
当double_encode关闭PHP将不存在的HTML实体编码打开,默认是把一切。


<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

参考实例

<?php

function get_page($url)
{
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, 'some bot');
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_REFERER, '-');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
...
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_NOBODY, 0);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  $html = curl_exec($curl);
  curl_close($curl);

  return $html;
}

$text = get_page($url);
$new = htmlspecialchars($text, ENT_QUOTES); // here is the magic :)

    echo '<pre>' .$new. '</pre>';


?>

 

php explode 实例与教程
array explode ( string separator, string string [, int limit] )

separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的最大数目。


每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。

如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包

含 string 的剩余部分。

如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包

含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。

在本例中,我们将把字符串分割为数组:

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
实例二

<?php
//利用 explode 函数分割字符串到数组
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$hello = explode(',',$source);

for($index=0;$index<count($hello);$index++){
echo $hello[$index];echo "</br>";
}

实例三

<?php
$foo = 'uno dos  tres'; // two spaces between "dos" and "tres"
print_r(explode(' ', $foo));
?>

Array
(
    [0] => uno
    [1] => dos
    [2] =>
    [3] => tres
)
请记住,explode()可以返回,如果分隔立即重复两次(或更多空元素),如以下示

例所示:

PHP implode() 函数实例教程

  1. <?php
  2. /**
  3. * @file class.126http.php
  4. * 获得126邮箱通讯录列表
  5. * @author jvones<jvones@gmail.com> http://www.jvones.com/blog
  6. * @date 2009-09-26
  7. **/
  8.  
  9. class http126
  10. {
  11.  
  12.         private function login($username, $password)
  13.         {               
  14.                 //第一步:初步登陆
  15.                 $cookies = array();
  16.                 $ch = curl_init();
  17.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  18.                
  19.                 curl_setopt($ch, CURLOPT_URL, "https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1");
  20.                 curl_setopt($ch, CURLOPT_POST, 1);
  21.                 curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."@126.com&password=".$password);
  22.                
  23.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
  24.                 curl_setopt($ch,CURLOPT_HEADER,1);               
  25.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  26.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27.                 $str = curl_exec($ch);        
  28.                 //file_put_contents('./126result.txt', $str);               
  29.                 curl_close($ch);
  30.                         
  31.                 //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址
  32.                 preg_match("/replace("(.*?)");/", $str, $mtitle);
  33.                 $_url1 = $mtitle[1];
  34.                
  35.                 //file_put_contents('./126resulturl.txt', $redirect_url);        
  36.                 //第二步:再次跳转到到上面$_url1
  37.                 $ch = curl_init($_url1);               
  38.                
  39.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  40.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  41.                 curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);
  42.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);               
  43.                 curl_setopt($ch,CURLOPT_HEADER,1);        
  44.                 $str2 = curl_exec($ch);
  45.                 curl_close($ch);
  46.                                                 
  47.                 if (strpos($contents, "安全退出") !== false)
  48.                 {                        
  49.                         return 0;
  50.                 }               
  51.                 return 1;
  52.         }
  53.         
  54.         /**
  55.          * 获取邮箱通讯录-地址
  56.          * @param $user
  57.          * @param $password
  58.          * @param $result
  59.          * @return array
  60.          */
  61.         public function getAddressList($username, $password)
  62.         {               
  63.                 if (!$this->login($username, $password))
  64.                 {
  65.                         return 0;
  66.                 }
  67.             
  68.                 $header = $this->_getheader($username);
  69.                 if (!$header['sid'])
  70.         {
  71.             return 0;
  72.         }
  73.         
  74.         //测试找出sid(很重要)和host
  75.         //file_put_contents('./host.txt', $header['host']);
  76.         //file_put_contents('./sid.txt', $header['sid']);
  77.         
  78.                 //开始进入模拟抓取
  79.                 $ch = curl_init();
  80.                 curl_setopt($ch, CURLOPT_URL, "http://".$header['host']."/a/s?sid=".$header['sid']."&func=global:sequential");
  81.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
  82.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
  83.                 $str = "<?xml version="1.0"?><object><array name="items"><object><string name="func">pab:searchContacts</string><object name="var"><array name="order"><object><string name="field">FN</string><boolean name="ignoreCase">true</boolean></object></array></object></object><object><string name="func">user:getSignatures</string></object><object><string name="func">pab:getAllGroups</string></object></array></object>";
  84.                 curl_setopt($ch, CURLOPT_POST, 1);
  85.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  86.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  87.                 ob_start();
  88.                 curl_exec($ch);
  89.                 $contents = ob_get_contents();
  90.  
  91.                 ob_end_clean();
  92.                 curl_close($ch);
  93.                
  94.         //get mail list from the page information username && emailaddress
  95.         preg_match_all("/<strings*name="EMAIL;PREF">(.*)</string>/Umsi",$contents,$mails);
  96.         preg_match_all("/<strings*name="FN">(.*)</string>/Umsi",$contents,$names);
  97.         $users = array();
  98.         foreach($names[1] as $k=>$user)
  99.         {
  100.             //$user = iconv($user,'utf-8','gb2312');
  101.             $users[$mails[1][$k]] = $user;
  102.         }
  103.         if (!$users)
  104.         {
  105.             return '您的邮箱中尚未有联系人';
  106.         }      
  107.         
  108.         return $users;
  109.         }
  110.         
  111.         /**
  112.     * Get Header info
  113.     */
  114.     private function _getheader($username)
  115.     {
  116.                 $ch = curl_init();
  117.                 curl_setopt($ch, CURLOPT_URL, "http://entry.mail.126.com/cgi/ntesdoor?hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1&username=".$username."@126.com");
  118.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);  //当前使用的cookie
  119.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   //服务器返回的新cookie
  120.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  121.                 curl_setopt($ch, CURLOPT_HEADER, true);
  122.                 curl_setopt($ch, CURLOPT_NOBODY, true);
  123.                 $content=curl_exec($ch);
  124.                
  125.                 preg_match_all('/Location:s*(.*?)rn/i',$content,$regs);
  126.         $refer = $regs[1][0];
  127.         preg_match_all('/http://(.*?)//i',$refer,$regs);               
  128.         $host = $regs[1][0];
  129.         preg_match_all("/sid=(.*)/i",$refer,$regs);
  130.         $sid = $regs[1][0];
  131.                
  132.                 curl_close($ch);
  133.                 return array('sid'=>$sid,'refer'=>$refer,'host'=>$host);
  134.     }
  135. }
  136.  
  137. ?>
[!--infotagslink--]

相关文章