preg_replace_callback 正则替换回调方法用法,
Example #1 preg_replace_callback() 和 匿名函数
<?php
/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>\s*\w|',
function ($matches) {
return strtolower($matches[0]);
},
$line
);
echo $line;
}
fclose($fp);
?>
Example #2 preg_replace_callback()示例 <?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
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); ?>
Example #3 preg_replace_callback()使用递归构造处理BB码的封装 <?php
$input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"; function parseTagsRecursive($input)
{
/* 译注: 对此正则表达式分段分析
* 首尾两个#是正则分隔符
* \[indent] 匹配一个原文的[indent]
* ((?:[^[]|\[(?!/?indent])|(?R))+)分析:
* (?:[^[]|\[(?!/?indent])分析:
* 首先它是一个非捕获子组
* 两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent.
* (?R) 正则表达式递归
* \[/indent] 匹配结束的[/indent]
* / $regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'; if (is_array($input)) {
$input = '<div style="margin-left: 10px">'.$input[1].'</div>';
} return preg_replace_callback($regex, 'parseTagsRecursive', $input);
} $output = parseTagsRecursive($input); echo $output;
?>
preg_replace_callback 正则替换回调方法用法,的更多相关文章
- PHP中正则替换函数preg_replace用法笔记
今天应老板的需求,需要将不是我们的页面修改一个链接,用js+iframe应该也能实现,但是我想尝试一下php实现方法. 首先你得先把别人的页面download到你的php中,实现方法可以用curl, ...
- PHP模板引擎正则替换函数 preg_replace 与 preg_replace_callback 使用总结
在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换). 详情介绍参考博文:P ...
- Python正则替换字符串函数re.sub用法示例(1)
本文实例讲述了Python正则替换字符串函数re.sub用法.分享给大家供大家参考,具体如下: python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串然后把它替 ...
- 缓存需要注意的问题以及使用.net正则替换字符串的方法
参考资料:http://www.infoq.com/cn/news/2015/09/cache-problems 正则替换字符串的简单方法: var regTableType = new Regex( ...
- 正则替换replace中$1的用法以及常用正则
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replacement)参数一 ...
- 正则替换replace中$1的用法
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...
- [转载]js正则表达式/replace替换变量方法
原文地址:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html JavaScript正则实战(会根据最近写的不断更新) 1.j ...
- js正则表达式/replace替换变量方法
转自:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html 1. javascript 正则对象替换创建和用法:/patter ...
- Python正则式的基本用法
Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...
随机推荐
- win8.1和wp8.1共用代码,需要注意的一些问题
最近写了一个应有,使用了mvvmlight,把viewmodel.model.common之类的代码都放到了shared共享,写下来才发现,有不少问题是自已下手之前没注意到的,有些地方实在没法中途改了 ...
- vuejs属性绑定和双向绑定
属性绑定 html <div v-bind:title="title">hello world</div> js new Vue({ el:'#root', ...
- Ribbon 负载均衡搭建
本机IP为 192.168.1.102 1. 新建Maven 项目 ribbon 2. pom.xml <project xmlns="http://maven.ap ...
- CSS 样式、布局、盒子模型
Css内容: 常用样式: 字体 颜色 背景 布局: 浮动 定位 标签特性 标签盒子模型: 边距 边框 动画: 旋转 渐变 注意:Css引路径从css文件里找 Html和js ...
- js常见问题总结归纳
一.使用 typeof bar === "object" 来确定 bar 是否是对象的潜在陷阱是什么?如何避免这个陷阱? 首先typeof bar === "object ...
- c#:无限极树形结构
最近一直在研究树形结构菜单,无意中让我弄了出来.先上代码: 首先需要这个的一个类 public class Tree { public int id { get; set; } public stri ...
- 如何禁止用户连续点击一个按钮事件详细JS
<input type="button" id="submit" value="提交"> <script> $(do ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器
<?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...
- Dungeon Master(逃脱大师)-BFS
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- usb gadge驱动设计之我是zero
此处将以zero.c为例进行讲解. 第一次接触zero.c驱动的时候,是因为某项目需要,提供一种usb字符设备,希望能够通过字符设备打开,读取和发送文件.当时能想到的就是zero.c文件,本打算按照z ...