本文实例讲述了PHP正则preg_replace_callback函数的用法。分享给大家供大家参考。具体实现方法如下:
PHP正则表达式功能强大,本范例演示了preg_replace_callback函数的用法
/.../
$EntireSection = $Match[0];
// --> "\nTitle: Hello world!"
// Key
// --> ([a-z0-9]+)
$Key = $Match[1];
// --> "Title"
// Value
// --> ([^\n\r]+)
$Value = $Match[2];
// --> "Hello world!"
// Add some bold () tags to around the key to
return '' . $Key . ': ' . $Value;
}
// The regular expression will extract and pass all "key: value" pairs to
// the "RewriteText" function that is definied above
$NewText = preg_replace_callback('/[\r\n]([a-z0-9]+): ([^\n\r]+)/i',"RewriteText",$Text);
// Print the new modified text
print $NewText;
希望本文所述对大家的PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/21857.html