最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php mixed preg_replace_callback 实例应用代码
时间:2010-07-03 编辑:简简单单 来源:一聚教程网
//需求:在所有连接后面添加一个request=xxx; 这个函数比preg_replace灵活性更强,要注意它所替换的内容为整个正则表达式的内容。
$content = '链接1链接2';
function add_source($matches)
{
if(strpos($matches[1], '?'))
{
return 'href="'.$matches[1].'&request=xxx"'; //注意,这里和下面都加上了正则里括号外的东西:href="
}
else
{
return 'href="'.$matches[1].'?request=xxx"';
}
}
$content = preg_replace_callback('/href=['|"](.*?)['|"]/', 'add_source', $content);
//实例二
// 此文本是用于 2002 年的,
// 现在想使其能用于 2003 年
$text = "april fools day is 04/01/2002 ";
$text.= "last christmas was 12/24/2001 ";
// 回调函数
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);
// 结果为:
// april fools day is 04/01/2003
// last christmas was 12/24/2002
-
上一个: 通用mysql数据库连接类代码
-
下一个: php限制用户60秒后发布评论实例代码
相关文章
- thinkphp标签实现bootsrtap轮播carousel实例代码 04-17
- PHP单例模式定义与使用实例详解 03-16
- php观察者模式应用场景实例详解 03-09
- thinkphp怎么用model?ThinkPHP中实例Model方法的区别说明 02-24
- php怎么调用翻译?php有道翻译api调用方法实例 02-24
- PHP对象实例化单例方法 02-08