练习一:
<?php
$txt = "This is a link to http://www.google.com/";
$pattern1 = '/http:\/\/(.*)\//';
$pattern2 = '/http:\/\/(.*)\//e';
$replacement1 = "<a href='$0'>$1</a>";
$replacement2 = 'strtoupper("<a href=\'$0\'>$1</a>")';
echo preg_replace($pattern1, $replacement1, $txt);
//This is a link to <a href='http://www.google.com/'>www.google.com</a>
echo "<br />";
echo preg_replace($pattern2, $replacement2, $txt);
//This is a link to <A HREF='HTTP://WWW.GOOGLE.COM/'>WWW.GOOGLE.COM</A>
?>
练习二:
$txt = "sss <b> &";
$pattern = '/[&<">]/e';
$replacement = array('&' => '&', '<' => '<', '>' => '>', '"' => '"');
echo preg_replace($pattern, '$replacement["$0"]', $txt);
//sss <b> &
练习三:
$subjects = array("sss <b> &", "<b>aaa</b>");
$patterns = array('/&/', '/</', '/>/', '/"/');
$replacements = array('&', '<', '>', '"');
$rs_array = preg_replace($patterns, $replacements, $subjects);
print_r($rs_array);
/*
Array
(
[0] => sss <b> &
[1] => <b>aaa</b>
)
*/
preg_replace_callback函数
<?php
$txt = "vim --- vim -";
$pattern = '/\bvim\b/';
function fn_callback($matches) {
return "<b>" . $matches[0] . "</b>";
}
$rs = preg_replace_callback($pattern, "fn_callback", $txt);
echo $rs;
//<b>vim</b> --- <b>vim</b> -
?>
分享到:
相关推荐
首先向你介绍下PHP preg_replace,PHP preg_replace的使用是我们实现的方法,那么对于PHP正则表达式替换实现过程我们从实例入手。 PHP正则表达式替换的相关概念: preg_replace:执行正则表达式的搜索和替换 mixed...
在前面我们也说过,基于perl的正则表达式要快于POXIS正则表达式处理函数,所以我们只介绍以preg开头的基于perl的正则表达式。注意:在能偶使用字符串函数处理的时候,就不要使用正则表达式来处理字符串,因为字符串...
php正则表达式功能强大,本范例演示了preg_replace_callback函数的用法 // Define a dummy text, for testing... $Text = "Title: Hello world!\n"; $Text .= "Author: Jonas\n"; $Text .= "This is a example ...
本文实例讲述了PHP正则表达式函数preg_replace用法。分享给大家供大家参考,具体如下: preg_replace 执行一个正则表达式的搜索和替换 语法:preg_replace (pattern ,replacement ,subject,limit,count ) 参数 ...
preg_replace 可以清除html标签 可以配合str_replace 一起使用
介绍:preg_replace 执行正则表达式的搜索和替换,如果只是单纯的匹配字符串建议使用str_replace(),因为其执行效率高的多。mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) ...
本文实例讲述了PHP正则表达式匹配替换与分割功能。分享给大家供大家参考,具体如下: 正则表达式在PHP中的作用主要包括:分割、匹配、查找与替换。 匹配功能 preg_match_all 全部匹配函数 preg_match_all (string ...
正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于:•正则匹配:根据正则表达式匹配相应的内容•正则替换:根据正则表达式匹配内容并替换•正则分割:根据正则表达式分割字符串在 PHP 中有两类正则...
需要我们用程序处理的数据并不总是预先以数据库思维设计的,或者说是无法用数据库的结构去存储的。 比如模版引擎解析模版、垃圾敏感信息过滤等等... 代码如下:preg_replace (正则表达式, 替换成, 字符串, 最大替换次数
php preg_replace()替换实例讲解,php preg_replace()是执行一个正则表达式的搜索和替换的用函数.
filter (PHP 5 >= 5.3.0) preg_filter — 执行一个正则表达式搜索和替换 mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) preg_filter()等价于...
PHP正则表达式测试工具,封装PCRE函数,格式化输出,便于PHP正则表达式调试。 函数介绍 match: 正则匹配,执行preg_match并格式化输出 match_all: 正则匹配,执行preg_match_all并格式化输出 grep: 正则匹配,执行...
php正则表达式学习笔记分享: 1.创建正则表达式 $regex = ‘/\d/i’; 与JavaScript中的第一个方式有点像,只是这里的话是个字符串。 2.正则表达式中的特殊字符 特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < ...
主要介绍了PHP正则表达式处理函数(PCRE 函数),结合实例形式总结分析了php正则表达式preg_replace、preg_match、preg_match_all、preg_split及preg_quote等函数相关使用技巧,需要的朋友可以参考下
我们常常会用到PHP过滤一些标签的功能,比如过滤链接标签、过滤script标签等等,下面就介绍一下PHP过滤常用标签的正则表达式代码: $str=preg_replace(/\s+/, , $str); //过滤多余回车 $str=preg_replace(/<[ ]+...