ereg_replace
字串比对剖析并取代。
语法: string ereg_replace(string pattern, string replacement, string string);
传回值: 字串
函式种类: 资料处理
本函式以 pattern 的规则来剖析比对字串 string,欲取而代之的字串为参数 replacement。传回值为字串型态,为取代后的字串结果。
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 "$text\n"; // This is a fine day, not overcast and rainy. ?>
ken@freebsdrocks.com 并同时提出具有相同功能的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 "$text\n";
ereg() eregi() eregi_replace()
|