php返回在指定的字元前添加反斜線的字串函數addslashes()

來源:互聯網
上載者:User
一、addslashes()函數

1.addslashes()函數,是在指定的預定字元前加反斜線。文法:addslashes(str);

2.參數是一個字串

3.這些預定義字元有四種,是:單引號(’),雙引號(”)、反斜線(\)和NULL

4.例如:

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

12345678910

二、addcslashes()函數

1.addcslashes()函數,是在指定的字元前添加反斜線。

文法:addcslashes(str,chararcters);

2.中參數str是必須的,規定要檢查的字串,而character是可選的,規定受addcslashes()影響的字元或字元範圍。

3.例1:

<?php    $str="Hello,my name is John Adams.";    echo $str;   //輸出:Hello,my name is John Adams.echo addcslashes($str,'m');  //輸出:    Hello,\my na\me is John Ada\ms.echo addcslashes($str,'J');  //輸出:Hello,my name is \John Adams  ?>

123456789101112

例2、

<?php$str="Hello,my name is John Adams.";echo $str;  //輸出:Hello,my name is John Adams.echo addcslashes($str,'A..Z');  //輸出:\Hello,my name is \John \Adams.echo addcslashes($str,'a..z');  //輸出:H\e\l\l\o,\m\y \n\a\m\e \i\s J\o\h\n A\d\a\m\s.echo addcslashes($str,'a..h');  //輸出:H\ello,my n\am\e is Jo\hn A\d\ams.?>

1234567891011121314

註:addcslashes()函數,對於指定字元或者字元範圍是區分大小寫。

在字元 "W" 前添加反斜線:

<?php $str = addcslashes("Hello World!","W");echo($str); ?>

定義和用法

addcslashes() 函數返回在指定的字元前添加反斜線的字串。

注釋:addcslashes() 函數是區分大小寫。

注釋:在對 0(NULL)、r(斷行符號)、n(換行)、t(換頁)、f(定位字元)和 v(垂直定位字元)應用 addcslashes() 時要小心。在 PHP 中,\0、\r、\n、\t、\f 和 \v 是預定義的逸出序列。

文法

addcslashes(string,characters)

參數   描述

string 必需。規定要轉義的字串。

characters 必需。規定要轉義的字元或字元範圍。

技術細節 傳回值:

返回已轉義的字串。

向字串中的特定字元添加反斜線:

<?php$str = "Welcome to my humble Homepage!";echo $str."<br>";echo addcslashes($str,'m')."<br>";echo addcslashes($str,'H')."<br>";?>

執行個體 2

向字串中的一個範圍內的字元添加反斜線:

<?php$str = "Welcome to my humble Homepage!";echo $str."<br>";echo addcslashes($str,'A..Z')."<br>";echo addcslashes($str,'a..z')."<br>";echo addcslashes($str,'a..g');?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.