天天看點

PHP字元串替換幾種函數執行個體

  1. strtr()
  2. str_replace()
  3. substr_replace()
  4. preg_replace()

strtr

1

2

3

4

5

6

7

​$str​

​ ​

​= ​

​​

​"test"​

​;​

​$str1​

​= ​

​strtr​

​(​

​$str​

​, ​

​'t'​

​, ​

​'z'​

​); ​

​// zesz​

​$str2​

​= ​

​strtr​

​(​

​$str​

​, ​

​'tt'​

​, ​

​'z1'​

​);​

​// 1es1​

​$str3​

​= ​

​strtr​

​(​

​$str​

​, ​

​'t'​

​, ​

​''​

​);​

​// test​

​$str4​

​= ​

​strtr​

​(​

​$str​

​, ​

​'ts'​

​, ​

​'12'​

​);​

​// 1e21​

​$str5​

​= ​

​strtr​

​(​

​$str​

​, ​

​array​

​(​

​"t"​

​=> ​

​''​

​)); ​

​// es​

​$str6​

​= ​

​strtr​

​(​

​$str​

​, ​

​array​

​(​

​"e"​

​=> ​

​'www'​

​, ​

​"s"​

​=> ​

​"hhh"​

​));​

​// twwwhhht​

str_replace

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

​$str​

​= ​

​"test"​

​;​

​$str1​

​= ​

​str_replace​

​(​

​'t'​

​, ​

​''​

​, ​

​$str​

​);​

​// es​

​$str2​

​= ​

​str_replace​

​(​

​'es'​

​, ​

​'t'​

​, ​

​$str​

​);​

​// ttt​

​$str3​

​= ​

​str_replace​

​(​

​array​

​(​

​'t'​

​, ​

​'sa'​

​), ​

​array​

​(​

​'a'​

​, ​

​'b'​

​), ​

​$str​

​);​

​// aeb​

​$str4​

​= ​

​str_replace​

​(​

​array​

​(​

​'t'​

​, ​

​'sa'​

​), ​

​array​

​(​

​'a'​

​), ​

​$str​

​);​

​// ae​

​$str5​

​= ​

​str_replace​

​(​

​array​

​(​

​'t'​

​), ​

​array​

​(​

​'a'​

​, ​

​'b'​

​), ​

​$str​

​);​

​// aesa​

​$str6​

​= ​

​str_replace​

​(​

​array​

​(​

​'t'​

​, ​

​'s'​

​), ​

​'e'​

​, ​

​$str​

​);​

​// eeee​

​$arr​

​= ​

​array​

​(​

​'a'​

​=> ​

​'testa'​

​,​

​'b'​

​=> ​

​'testb'​

​,​

​'c'​

​=> ​

​array​

​(​

​'ca'​

​=> ​

​'tes1'​

​,​

​'cb'​

​=> ​

​'tes2'​

​,​

​),​

​);​

​$arr1​

​= ​

​str_replace​

​(​

​'es'​

​, ​

​'t'​

​, ​

​$arr​

​);​

​/**​

​$arr1 = array(​

​'a' => 'ttta',​

​'b' => 'tttb',​

​'c' => array(​

​'ca' => 'tes1',​

​'cb' => 'tes2',​

​),​

​);​

​*​

​*​

​*/​

substr_replace

​$str​

​= ​

​"test"​

​;​

​echo​

​substr_replace(​

​$str​

​, ​

​'zzz'​

​, 1, 2);​

​// tzzzt​

​$arr​

​= ​

​array​

​(​

​'a'​

​=> ​

​'what'​

​,​

​'b'​

​=> ​

​'are'​

​,​

​'c'​

​=> ​

​array​

​(​

​'ca'​

​=> ​

​'11'​

​,​

​'cb'​

​=> ​

​'22'​

​,​

​),​

​);​

​$arr1​

​= substr_replace(​

​$arr​

​, ​

​'ok'​

​, 0, 1);​

​/**​

​$arr1 = array(​

​'a' => 'okhat',​

​'b' => 'okre',​

​'c' => 'okarry'​

​);​

​*/​

preg_replace

​$arr​

​= ​

​array​

​(​

​'a'​

​=> ​

​'XXX25012349999XXX'​

​,​

​// 不是以1開頭​

​'b'​

​=> ​

​'XXX13188887777XXX'​

​,​

​'c'​

​=> ​

​'XXXX918811113333XXXXX'​

​,​

​'d'​

​=> ​

​'XXXX188111133331XXXXX'​

​,​

​);​

​$re​

​= preg_replace(​

​'/([^0-9])(1\d{10})([^0-9])/'​

​, ​

​'$1 mobile:$2 $3'​

​, ​

​$arr​

​);​

​// 利用$1,$3​

​$re1​

​= preg_replace(​

​'/(?<=[^0-9])(1\d{10})(?=[^0-9])/'​

​, ​

​' mobile:$1 '​

​, ​

​$arr​

​);​

​// 使用向前向後比對,直接比對的$1電話号​

​/**​

​Array​

​(​

​[a] => XXX25012349999XXX​

​[b] => XXX mobile:13188887777 XXX​

​[c] => XXXX918811113333XXXXX​

​[d] => XXXX188111133331XXXXX​

​)​

​**/​

有兩種傳遞參數的格式:一種是數組k=>v形式,一種是兩個字元串的形式。

一,字元串情況   string strtr ( string $str , string $from , string $to ):

  search參數的每個字元和replace參數的字元對應(這個是該函數用法關鍵)。如果search中出現相同的字元,會按照search中最後一個字元對應的replace參數的字元代替(如第6行代碼)。

  如果search或者replace是空字元串,則不會被替換,原樣輸出(如第7行)。

二,數組k=>v情況   string strtr ( string $str , array $replace_pairs ):

  這種情況比較簡單,就是把字元串中的k替換成v

str_replace()的用法:  mixed str_replace ( mixed $search , mixed $replace , mixed $subject)

str_replace()在使用上比較簡單:

  字元串上的使用,replace直接替換掉search,注意這裡就不是字元對應,是倆參數字元串對應(差別于strtr()函數)。

  數組上的使用(值得一提的是:前一個對應元素替換完得到的結果再應用到下一個對應元素【會發現第9行的sa被替換成空了,而sa是第一步替換完得到的子串】。):

search和replace都是數組的時候:如果是兩個數組元素個數相等,各個參數對應(如第8行)。如果search個數大于replace的話,多餘的就被空字元串替換(如第9行)。

search是數組,replace是字元串的話,就是字元串中出現的數組各個值被被replace替換(如第11行)。

比較意外的用法:str_replace()可以處理數組的值,結果也傳回數組。。這個就可以免去程式員手動循環數組了,經測試,隻能對一維數組生效(如第22行)。

substr_replace()用于替換字元串的子串。使用方法:是指定處理字元串的開始和長度,然後替換掉這段區間。也可以替換數組。。

  mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

數組替換的時候,也是處理一維數組的字元串值。。

preg_replace()的用法

正則這個就尼瑪太強大了,主要用于比對特定格式的時候,否則推薦直接使用str_replace()函數。