php教程 mixed preg_replace_callback 实例应用代码
//需求:在所有连接后面添加一个request=xxx; 这个函数比preg_replace灵活性更强,要注意它所替换的内容为整个正则表达式的内容。
$content = 'http://www.bkjia.com/aaa.php?id=111">链接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/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);
// 结果为:
// april fools day is 04/01/2003
// last christmas was 12/24/2002

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具