找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 858|回复: 0
打印 上一主题 下一主题

php正则preg_replace_callback函数用法实例

[复制链接]

2536

主题

2536

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
跳转到指定楼层
楼主
发表于 2018-2-14 05:45:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了php正则preg_replace_callback函数的用法。分享给大家供大家参考。具体实现方法如下:
php正则表达式功能强大,本范例演示了preg_replace_callback函数的用法
// Define a dummy text, for testing...
$Text = "Title: Hello world!\n";
$Text .= "Author: Jonas\n";
$Text .= "This is a example message!\n\n";
$Text .= "Title: Entry 2\n";
$Text .= "Author: Sonja\n";
$Text .= "Hello world, what's up!\n";
// This function will replace specific matches
// into a new form
function RewriteText($Match){
  // Entire matched section:
  // --> /.../
  $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程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • php正则之函数 preg_replace()参数说明
  • PHP 正则表达式常用函数使用小结
  • PHP正则表达式基本函数 修饰符 元字符和需转义字符说明
  • PHP 字符串正则替换函数preg_replace使用说明
  • PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
  • PHP 正则表达式常用函数
  • PHP正则替换函数preg_replace和preg_replace_callback使用总结
  • php中正则替换函数ereg_replace用法实例
  • PHP中preg_match函数正则匹配的字符串长度问题
  • PHP preg match正则表达式函数的操作实例
  • php常用正则函数实例小结
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端