$subject = "From ab_cD efg";
$callback = function($match)
{
//var_dump($match);
//echo $match[1];
return "FROM " . strtolower($match[1]) . " ";
//return preg_replace($match[1], strtolower($match[1]) ,$match[0]);
};
echo preg_replace_callback('/from\s+([\w]+)\s*/i', $callback, $subject, 1);
echo "\n";
echo preg_replace_callback('/from\s+([\w]+)\s*/i', $callback, "From ab_cD", 1);
echo "\n";
echo preg_replace_callback('/from\s+([\w]+)\s*/i', $callback, "Fsssc ab_cD", 1);
ref
http://tools.jb51.net/regex/create_reg
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002n";
$text.= "Last christmas was 12/24/2001n";
// 回调函数
function next_year($matches)
{
// 通常: $matches[0]是完成的匹配
// $matches[1]是第一个捕获子组的匹配
// 以此类推
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(d{2}/d{2}/)(d{4})|",
"next_year",
$text);
沒有留言:
張貼留言