PHP:preg_replace
2014-04-29 18:10
253 查看
关于preg_match: /article/6194017.html
关于preg_match_all:暂时没有完整的
preg_replace_callback核心例子1:把<a>标签的文本中的<替换为空,文本的其它部分不变
preg_replace: 能够将匹配的模式处理成目标模式。
任务:把处理为<img>100</img>
代码:
输出:
关于括号的用法,看下最后两个任务:/article/6194017.html
代码2:
代码3:
代码4:
代码5:
代码6:
实验:转换以下字符串
aaa
转换规则是:两个标签内的链接,如果是在.360.cn域,则信任,否则过滤url
上述的字符串转换后为:aaa
实现代码:
关于preg_match_all:暂时没有完整的
preg_replace_callback核心例子1:把<a>标签的文本中的<替换为空,文本的其它部分不变
$note = '<a href="http://www.so.com"><>ab<></a><a href="http://www.so.com"><>ab<></a>'; function filter_subject($matches){ return $matches[1].(str_replace(array("<", ">"), array(" ", " "), $matches[2])) . $matches[3]; } $note = preg_replace_callback('/(<a href.*?>)(.*?)(<\/a>)/', "filter_subject", $note); 输出:"<a href="http://www.so.com"> ab </a><a href="http://www.so.com"> ab </a>"
preg_replace: 能够将匹配的模式处理成目标模式。
任务:把处理为<img>100</img>
代码:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/', '<img>$1</img>', $str); var_dump($newstr);
输出:
string(37) "<img>100</img>test.png<img>1000</img>"
关于括号的用法,看下最后两个任务:/article/6194017.html
代码2:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/', '<img>\1</img>', $str); var_dump($newstr);
代码3:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/', "<img>\\1</img>", $str); var_dump($newstr);
代码4:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/e', "parse_image($1)", $str); var_dump($newstr); function parse_image($str) { return '<img>'.$str.'</img>'; }
代码5:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/', parse_image('$1'), $str); var_dump($newstr); function parse_image($str) { return '<img>'.$str.'</img>'; }
代码6:
<?php $str = 'test.png'; $newstr = preg_replace('/\[img\](.*?)\[\/img\]/', parse_image("$1"), $str); var_dump($newstr); function parse_image($str) { return '<img>'.$str.'</img>'; }
实验:转换以下字符串
aaa
转换规则是:两个标签内的链接,如果是在.360.cn域,则信任,否则过滤url
上述的字符串转换后为:aaa
实现代码:
相关文章推荐
- PHP之preg_replace()与ereg_replace()正则匹配比较讲解
- PHP字符串正则替换函数preg_replace
- PHP 正则表达式替换 preg_replace 函数
- (PHP)正则表达式-preg_replace()和preg_filter()
- PHP preg_replace() 正则替换所有符合条件的字符串示例
- php正则之函数 preg_replace()参数说明
- php中preg_replace()和ereg_replace()函数的用法区别
- PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
- 详解PHP正则表达式替换实现(PHP preg_replace,PHP preg_replace)
- php 中 str_replace或者preg_replace对php代码的性能的影响
- PHP正则preg_replace()函数 Unknown modifier 'c'问题 _新手学php正则的时候遇到的问题
- php preg_replace空格无法替换问题
- PHP正则表达式的逆向引用与子模式 php preg_replace应用
- 使用thinkphp和php中的问题-preg_replace(), 链式查询
- preg_replace的简单用法php正则替换
- php正则表达式函数 preg_replace用法
- php preg_match_all结合str_replace替换内容中所有img
- PHP preg_replace() 正则替换所有符合条件的字符串
- PHP preg_replace
- smarty如何完美兼容php5.5+和preg_replace_callback如何替换preg_replace