函式描述
string Ereg_replace(string pattern,string replacement,string string)
返回值
函式ereg_eplace返回替換後的字元串pattern。
ereg_replace() 例子
<?php
$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "\\1was", $string);
echo ereg_replace("(( )is)", "\\2was", $string);
?>
要注意的一點是如果在 replacement 參數中使用了整數值,則可能得不到所期望的結果。這是因為 ereg_replace() 將把數字作為字元的序列值來解釋並套用之。
ereg_replace() 例子
<?php
/* 不能產生出期望的結果 */
$num = 4;
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string; /* Output: 'This string has words.' */
/* 本例工作正常 */
$num = '4';
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string; /* Output: 'This string has 4 words.' */
?>
將 URL 替換為超連線
<?php
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $text);
?>