php的逸出字元串

來源:互聯網
上載者:User

今天碰到一個處理檔案特殊字元的事情,再次注意到這個問題,在php中:

* 以單引號為定界符的php字串,支援兩個轉義/'和//
* 以雙引號為定界符的php字串,支援下列轉義:
    /n 換行(LF 或 ASCII 字元 0x0A(10))
    /r 斷行符號(CR 或 ASCII 字元 0x0D(13))
    /t 水平定位字元(HT 或 ASCII 字元 0x09(9))
    // 反斜線
    /$ 貨幣符號
    /" 雙引號
    /[0-7]{1,3}               此Regex序列匹配一個用八進位符號表示的字元 
    /x[0-9A-Fa-f]{1,2}  此Regex序列匹配一個用十六進位符號表示的字元 

舉幾個例子:

一個包含/0特殊字元的例子:

$str = "ffff/0ffff";
echo(strlen($str));
echo("/n");
for($i=0;$i<strlen($str);$i++)echo("/t".ord($str{$i}));
echo("/n");

輸出結果:
----------------------

9
        102     102     102     102     0       102     102     102     102

替換特殊字元的例子

$str = "ffff/0ffff";
$str = str_replace("/x0", "", $str); 
//或者用$str = str_replace("/0", "", $str);
//或者用$str = str_replace(chr(0), "", $str);
echo(strlen($str));
echo("/n");
for($i=0;$i<strlen($str);$i++)echo("/t".ord($str{$i}));
echo("/n");
輸出結果:
----------------------
8
        102     102     102     102     102     102     102     102

八進位ascii碼例子:

//注意,符合正則/[0-7]{1,3}的字串,表示一個八進位的ascii碼。
$str = "/0/01/02/3/7/10/011/08/8";  //這裡的/8不符合要求,被修正為"//8" (ascii為92和56)
echo(strlen($str));
echo("/n");
for($i=0;$i<strlen($str);$i++)echo("/t".ord($str{$i}));
echo("/n");
輸出結果:
----------------------
11
        0       1       2       3       7       8       9       0       56      92      56

十六進位ascii碼例子:

$str = "/x0/x1/x2/x3/x7/x8/x9/x10/x11/xff";
echo(strlen($str));
echo("/n");
for($i=0;$i<strlen($str);$i++)echo("/t".ord($str{$i}));
echo("/n");
輸出結果:
----------------------
10
        0       1       2       3       7       8       9       16      17      255

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.