首頁  >  文章  >  後端開發  >  php ereg_replace函數

php ereg_replace函數

高洛峰
高洛峰原創
2016-11-29 16:08:421155瀏覽

$string = "this is a test"; 

echo str_replace(" is", " was", $string); 

echo ereg_replace(",1); 1就是第一個括號中空格 

echo ereg_replace("(( )is)", "2was", $string); //其中2就是第二個括號的空格,上面三行也就是把" is"替換為" was";都有空格的。

ereg_replace ( string pattern, string replacement, string string)

也就是說如果pattern中帶有()得字符串(如;括號中有空格),那你的replacement就可以使用如1的字符串,那這個1就給可以用你第1個括號中字串來替換,如果是2那就用第2個括號中的字串替換.

括號從左到右,以左括號為準,下面那個是去掉url中的如&page=1符字串比對解析並取代.

語法: string ereg_replace(string pattern, string replacement, string string);

回傳值:字串

函數種類類型:資料處理內容說明:本函數以pattern 的規則來解析比對字串string,欲取而代之的字串為參數replacement,傳回值為字串型別,為取代後的字串結果.

使用範例,ken@freebsdrocks. com 在16-mar-1999 提出的例子,代碼如下:

$text = 'this is a {1} day, not {2} and {3}.'; {1} day, not {2} and {3}.'; ( 1 => 'fine', 

                  2 =>   => 'rainy' ); 

while (ereg ('{([0-9]+)}', $text, $ regs)) { 

  $found = $regs[1]; 

  $text = ereg_replace("{".$found.")",1$daytype[$d "$textn"; 

// this is a fine day, not overcast and rainy.  

?> 

ken@freebsdrocks.com 並同時提出相同的程序功能is a {1} day, not {2} and {3}.'; 

%daytype = ( 1 => 'fine',      

             3 => 'rainy' );

$text =~ s/{(d+)}/$daytype{$1}/eg; 

print "$textn"; 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn