php正则preg_replace_callback函数用法实例

yipeiwu_com6年前PHP代码库

本文实例讲述了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 (<b>) tags to around the key to
  return '<b>' . $Key . '</b>: ' . $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加密之discuz内容经典加密方式实例详解

本文实例讲述了php加密之discuz内容经典加密方式。分享给大家供大家参考,具体如下: 导读:有的时候,我们希望对表里的某些敏感字段进行加密,想了好长时间没有比较好的解决方案,后台在网...

php实现微信模拟登陆、获取用户列表及群发消息功能示例

本文实例讲述了php实现微信模拟登陆、获取用户列表及群发消息功能。分享给大家供大家参考,具体如下: <?php header('Content-Type: text/h...

PHP中include()与require()的区别说明

require 的使用方法如 require("MyRequireFile.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指...

Asp.net 文本框全选的实现

一、鼠标滑过textbox全选 前台: <asp:TextBox runat="server" onMouseOver="this.focus();this.select()"&g...

动易数据转成dedecms的php程序

PS:这个可以举一反三,如新云或NB等asp文章系统都可转为dedecms或其它php的cms 复制代码 代码如下:<?php  //读取mdb数据库例程&nb...