摘要:$string="thisisatest";echostr_replace("is","was",$string);echoereg_replace("()is","1was",$string); 其中1就是第一個括號...
轉(zhuǎn)載請注明來源:php ereg_replace函數(shù)
- $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);
返回值:字符串
函數(shù)種類:資料處理
內(nèi)容說明:本函數(shù)以 pattern 的規(guī)則來解析比對字符串 string,欲取而代之的字符串為參數(shù) replacement,返回值為字符串類型,為取代后的字符串結(jié)果.
使用范例,ken@freebsdrocks.com 在 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.
- ?>
ken@freebsdrocks.com 并同時提出具有相同功能的perl 程序范例如下:
轉(zhuǎn)載請注明來源:php ereg_replace函數(shù)
- $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";
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。
