// preg_replace
// 刪除空格
$str = 'runo o b';
$str = preg_replace('/\s+/', '', $str);
// 將會改變爲'runoob'
echo $str;
// preg_replace_callback
// 將文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回調函數
function next_year($matches)
{
// 通常: $matches[0]是完成的匹配
// $matches[1]是第一個捕獲子組的匹配
// 以此類推
// print_r($matches);
return $matches[1].($matches[2]+1);// 替換完成匹配,也就是$matches[0]
}
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
"next_year",
$text);