在php中字符替換函數(shù)有幾個(gè)如有:str_replace、substr_replace、preg_replace、preg_split、str_split等函數(shù),下面我來給大家總結(jié)介紹介紹.
一、str_replace(find,replace,string,count)
作用:str_replace() 函數(shù)使用一個(gè)字符串替換字符串中的另一些字符。
參數(shù) 描述
find 必需,規(guī)定要查找的值.
replace 必需,規(guī)定替換 find 中的值的值.
string 必需,規(guī)定被搜索的字符串.
count 可選,一個(gè)變量,對(duì)替換數(shù)進(jìn)行計(jì)數(shù).
在本例中,我們將演示帶有數(shù)組和 count 變量的 str_replace() 函數(shù),代碼如下:
- <?php
- $arr = array("blue","red","green","yellow");
- print_r(str_replace("red","pink",$arr,$i));
- echo "Replacements: $i";
- ?>
- //輸出:
- Array
- (
- [0] => blue
- [1] => pink
- [2] => green
- [3] => yellow
- )
Replacements:1
補(bǔ)充:count如果被指定,它的值將被設(shè)置為替換發(fā)生的次數(shù).
二、substr_replace(string,replacement,start,length)
作用:substr_replace() 函數(shù)把字符串的一部分替換為另一個(gè)字符串.
參數(shù) 描述
string 必需,規(guī)定要檢查的字符串.
replacement 必需,規(guī)定要插入的字符串.
start 必需,規(guī)定在字符串的何處開始替換.
正數(shù) - 在第 start 個(gè)偏移量開始替換
負(fù)數(shù) - 在從字符串結(jié)尾的第 start 個(gè)偏移量開始替換
0 - 在字符串中的第一個(gè)字符處開始替換
charlist 可選,規(guī)定要替換多少個(gè)字符.
正數(shù) - 被替換的字符串長(zhǎng)度
負(fù)數(shù) - 從字符串末端開始的被替換字符數(shù)
0 - 插入而非替換
例子,代碼如下:
- <?php
- echo substr_replace("Hello world","earth",6);
- ?>
- //輸出:Hello earth
三、preg_replace(pattern,replacement,subject,limit = -1,$count)
作用:執(zhí)行一個(gè)正則表達(dá)式的搜索和替換
參數(shù) 描述
pattern 必需,需要搜索的模式.
replacement 必需,用于替換的字符串或數(shù)組.
subject 必需,需要替換的字符串或數(shù)組.
limit 替換的次數(shù),-1為無限
count 完成替換的次數(shù),變量
Example #1 使用后向引用緊跟數(shù)值原文,代碼如下:
- <?php
- $string = 'April 15, 2003';
- $pattern = '/(w+) (d+), (d+)/i';
- $replacement = '${1}1,$3';
- echo preg_replace($pattern, $replacement, $string);
- ?>
- //以上例程會(huì)輸出:
- April1,2003
Example #2 preg_replace()中使用基于索引的數(shù)組,代碼如下:
- <?php
- $string = 'The quick brown fox jumped over the lazy dog.';
- $patterns = array();
- $patterns[0] = '/quick/';
- $patterns[1] = '/brown/';
- $patterns[2] = '/fox/';
- $replacements = array();
- $replacements[2] = 'bear';
- $replacements[1] = 'black';
- $replacements[0] = 'slow';
- echo preg_replace($patterns, $replacements, $string);
- ?>
- //以上例程會(huì)輸出:
- The bear black slow jumped over the lazy dog.
四、preg_split ( pattern , subject,limit = -1 ,flag )
作用:通過正則表達(dá)式分割字符串
參數(shù) 描述
pattern 必需,需要搜索的模式.
replacement 必需,用于替換的字符串或數(shù)組.
subject 必需,需要替換的字符串.
limit 被分割的字符串最多l(xiāng)imit.
flag 模式
例 1672. preg_split() 例子,取得搜索字符串的成分,代碼如下:
- <?php
- // split the phrase by any number of commas or space characters,
- // which include " ", r, t, n and f
- $keywords = preg_split ("/[s,]+/", "hypertext language, programming");
- ?>
例 1673,將字符串分割成字符,代碼如下:
- <?php
- $str = 'string';
- $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
- print_r($chars);
- ?>
例 1674,將字符串分割為匹配項(xiàng)及其偏移量,代碼如下:
- <?php
- $str = 'hypertext language programming';
- $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
- print_r($chars);
- ?>
- //本例將輸出:
- Array
- (
- [0] => Array
- (
- [0] => hypertext
- [1] => 0
- )
- [1] => Array
- (
- [0] => language
- [1] => 10
- )
- [2] => Array
- (
- [0] => programming
- [1] => 19
- )
- )
五、str_split(subject,length)
作用:將字符串分割成數(shù)組
參數(shù) 描述
subject 字符串.
length 每一段的長(zhǎng)度.
例子1,代碼如下:
- <?php
- print_r(str_split("Hello"));
- ?>
- //輸出:
- Array
- (
- [0] => H
- [1] => e
- [2] => l
- [3] => l
- [4] => o
- )
例子2,代碼如下:
- <?php
- print_r(str_split("Hello",3));
- //開源代碼Vevb.com
- ?>
- 輸出:
- Array
- (
- [0] => Hel
- [1] => lo
- )
新聞熱點(diǎn)
疑難解答