php中幾個字串替換函式
一:strtr()的用法
$str = "test";
$str1 = strtr($str,'t','z'); // zesz
$str2 = strtr($str,'tt','z1'); // 1es1,注意這裡不是zesl,相同字元按照最後一個替換
$str3 = strtr($str,'t',''); // test,注意這裡不是es,替換為空子串會原樣輸出
$str4 = strtr($str,'ts','12'); // 1e21
$str5 = strtr($str,array("t" =>'')); // es,注意和上個空子串相比較
$str6 = strtr($str,array("e"=>'www',"s"=>"hhh"));// twwwhhht
二:str_replace()的用法
$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,注意第二個sa被空替換
$str5 = str_replace(array('t'), array('a', 'b'), $str);// aesa,注意最後一個b沒有生效
$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,注意位置是從0開始的,第一個數如果是負數表示從字串結尾的指定位置開始替換,第二個數表示長度
$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()的用法
正則功能強大...
$str = "test";
$str1 = strtr($str,'t','z'); // zesz
$str2 = strtr($str,'tt','z1'); // 1es1,注意這裡不是zesl,相同字元按照最後一個替換
$str3 = strtr($str,'t',''); // test,注意這裡不是es,替換為空子串會原樣輸出
$str4 = strtr($str,'ts','12'); // 1e21
$str5 = strtr($str,array("t" =>'')); // es,注意和上個空子串相比較
$str6 = strtr($str,array("e"=>'www',"s"=>"hhh"));// twwwhhht
二:str_replace()的用法
$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,注意第二個sa被空替換
$str5 = str_replace(array('t'), array('a', 'b'), $str);// aesa,注意最後一個b沒有生效
$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,注意位置是從0開始的,第一個數如果是負數表示從字串結尾的指定位置開始替換,第二個數表示長度
$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()的用法
正則功能強大...
相關文章
- php字串與字元替換函式PHP字串字元函式
- PHP字串替換substr_replace與str_replace函式PHP字串函式
- 替換空格 將一個字串中的空格替換成“ ”字串
- PHP中的字串函式PHP字串函式
- [php]幾個常用函式PHP函式
- SQL中的替換函式replace()使用SQL函式
- Vi中的替換字串字串
- javascript替換字串中的某個漢字JavaScript字串
- **PHP中替換換行符PHP
- 【Hive】字串替換函式translate和regexp_replaceHive字串函式
- js replace替換字串,同時替換多個方法JS字串
- PHP字串函式PHP字串函式
- PostgreSQL 查詢替換函式SQL函式
- PHP的字串函式PHP字串函式
- 字串的一個操作(替換類似陣列字串中的項)字串陣列
- 關於字串的幾個有用函式 (轉)字串函式
- sed 字串替換字串
- ACM 字串替換ACM字串
- 正規表示式的字串替換方法字串
- PHP 每日一函式 — 字串函式 crypt ()PHP函式字串
- PHP 每日一函式 — 字串函式 chr ()PHP函式字串
- PHP字串函式彙總PHP字串函式
- python函式教程:Python 字串操作(string替換、擷取等)Python函式字串
- linux中批量替換文字中字串Linux字串
- js字串中替換指定的的字元JS字串字元
- 【劍指offer】替換字串中的空格字串
- PHP 每日一函式 — 字串函式 addcslashes ()PHP函式字串
- PHP 每日一函式 — 字串函式 addslashes ()PHP函式字串
- vi替換字串(zz)字串
- POJ 3981 字串替換字串
- PHP如何替換多個字串不同位置不同長度的子串PHP字串
- oracle中的幾個hash函式Oracle函式
- MySQL字串函式 字串大小寫轉換MySql字串函式
- 002——php字串中的處理函式(一)PHP字串函式
- PHP 每日一函式 — 字串函式 count_chars ()PHP函式字串
- PHP 每日一函式 — 字串函式 crc32 ()PHP函式字串
- PHP 每日一函式 — 字串函式 chunk_split ()PHP函式字串
- php字串處理函式大全PHP字串函式