- $string = "this is a test";
- echo str_replace(" is", " was", $string);
- echo ereg_replace("( )is", "1was", $string); //其中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,返回值為字符串類型,為取代后的字符串結果.
使用范例,[email protected] 在 16-mar-1999 提出的例子,代碼如下:
- <?php
- $text = 'this is a {1} day, not {2} and {3}.';
- $daytype = array( 1 => 'fine',
- 2 => 'overcast',
- 3 => 'rainy' );
- while (ereg ('{([0-9]+)}', $text, $regs)) {
- $found = $regs[1];
- $text = ereg_replace("{".$found."}", $daytype[$found], $text);
- }
- echo "$textn";
- // this is a fine day, not overcast and rainy.
- ?>
[email protected] 并同時提出具有相同功能的perl 程序范例如下:
- $text = 'this is a {1} day, not {2} and {3}.';
- %daytype = ( 1 => 'fine',
- 2 => 'overcast',
- 3 => 'rainy' );
- $text =~ s/{(d+)}/$daytype{$1}/eg;
- print "$textn";
新聞熱點
疑難解答