您的位置 首页 php

PHP正则表达式核心技术完全详解 第5节 php正则替换函数

说到替换呢 这里我觉得首先要回忆一下字符串的相关替换函数!

str_replace() 在字符串中查找字符,然后替换成想要的字符 str_ireplace() 在字符串中查找字符,然后替换成想要的字符 (不区分大小写) 小提示 str_replace() 函数是全局替换

练习: 在字符串中查找数组对应的字符,设为红色 代码如下:

 //在字符串中查找数组对应的字符,设为红色
 
 $string='[北京市朝阳区HTTP://www.baidu.com
     天安门北京市朝阳区天安门
     北京市朝阳区HTTP://www.163.com天安门]';
 ​
 $data=[
     '#39;,
     '#39;,
     '#39;
 ];
 ​
 //处理之前
 echo nl2br($string);
 ​
 echo '<hr>';
 ​
 foreach ($data as $k=>$v){
     //这里使用的是不区分大小写的str_ireplace()方法来替换
     $string=str_ireplace($v, '<span style="color:red;">'.$v.'</span>', $string);
 }
 ​
 //处理之后
 echo nl2br($string);
 ​ 

str_replace()方法中的参数 有三种使用技巧 第一种: str_replace(string,string,string,替换次数); 代码如下:

 $string="online project php hosting user git includes php source-code php browser , 
 php in-line editing wikis and ticketing free for public php open-source code!";
 $search='php';
 $replace='<span style="color:#ff0000;">php</span>';
 echo str_replace($search, $replace, $string,$count);
 echo '<br>';
 echo '替换的次数是:'.$count.'次!'; 

第二种: str_replace(array,string,string,替换次数); 粗暴点 , 代码如下:

 $string="online project 美国 php hosting 日本 user git includes php 韩国 source-code php browser , 
 php in-line editing 法国 wikis and ticketing 澳大利亚 free for public php open-source code!";
 //查找的数据
 $search=array(
     '美国',
     '日本',
     '韩国',
     '法国',
     '澳大利亚'
 );
 //替换为
 $replace='<span style="color:#ff0000;">***</span>';
 ​
 //开始替换
 echo str_replace($search, $replace, $string,$count);
 echo '<br>';
 echo '替换的次数是:'.$count.'次!'; 

第三种: str_replace(array,array,string,替换次数); 代码如下:

 $string="online project 美国 php hosting 日本 user git includes php 韩国 source-code php browser , 
 php in-line editing 法国 wikis and ticketing 澳大利亚 free for public php open-source code!";
 ​
 //查找的数据
 $search=array(
     '美国',
     '日本',
     '韩国',
     '法国',
     '澳大利亚'
 );
 ​
 //替换为
 $replace=[
     '<span style="color:red;">***</span>',
     '<span style="color:blue;">???</span>',
     '<span style="color:yellow;">###</span>',
     '<span style="color:green;">@@@</span>',
     '<span style="color:#ccc;">&&&</span>',
 ];
 ​
 //开始替换
 echo str_replace($search, $replace, $string,$count);
 echo '<br>';
 echo '替换的次数是:'.$count.'次!'; 

以上就是回顾了一下 字符串中的替换,别走开,接下来我们才要进入正则函数替换的正题哦!

PHP正则替换函数 preg_replace() ; 正则中替换函数、返回值可能是一个字符串也可能是一个数组

  1. 正常使用 preg_replace(参数..) 参数列表: 参数1: 正则字符串或者正则数组参数​replacement替换为的字符串 或者 字符串数组 参数3: $string处理的字符串 或者 字符串数组 参数4: 每个模式在每个字符串上进行替换几次数,默认是 -1 全部替换 参数5: 记录替换次数的引用变量 代码案例如下:
 $string="online project php hosting  mysql user git includes php  source-code php browser 
 php in-line editing  wikis and ticketing  free for public javascript open-source code!";
 //查找的数据
 $search='/php | mysql | javascript/';
 //替换为
 $replace='<font color="red">***</font>';
 //开始替换
 echo preg_replace($search, $replace,$string); 

  1. 使用子模式进行替换 子模式可以用到第二个参数当中 代码案例如下:
 $string="online project php hosting  mysql user git includes php  source-code php browser , 
 php in-line editing  wikis and ticketing  free for public javascript open-source code!";
 //查找的数据
 $search='/(php | mysql | javascript)/';
 //替换为 这里要记得字符串的单双引号对反向引用的作用哦! 前面提到过 !!不懂的同学可以回去看看!!
 $replace="<font color='red'>\\1</font>";
 //开始替换
 echo preg_replace($search, $replace,$string); 

  1. 在前两个参数中都使用数组,可以一起将多个模式(正则)同时替换对应的多个值 替换UBB代码, 这里就不介绍UBB是什么了 自己百度一下就会知道简单地一匹 代码案例图如下:
     $string="online [align=left][b]PHP开发[/b][/align] project [u]php[/u] hosting  mysql user git includes php  source-code php browser , 
 php in-line [b]editing[/b]  wikis and ticketing  free for [font size=7]public[/font] javascript open-source code! A better way to work together
 [align=center][color color=red]GitHub[/color][/align] brings teams [color color=blue]together[/color] to work through problems, move ideas forward, and learn from each other along the way.
 [img width=100]#34;;
 ​
 //替换之前
 echo $string;
 echo '<hr>';
 ​
 //查找的数据
 $pattern=array(
     '/\[u\](.+)\[\/u\]/',
     '/\[b\](.+)\[\/b\]/',
     '/\[align=(left|center|right)\](.+)\[\/align\]/',
     '/\[font(\s+)?(size=\d)?\](.+)\[\/font\]/',
     '/\[color(\s+)?color=([a-zA-Z]+)?\](.+?)\[\/color\]/',
     '/\[img\s(width=\d{1,3})?\](.+)\[\/img\]/'
 );
 //替换为
 $replace=array(
     '<u>${1}</u>',
     '<b>${1}</b>',
     '<p align="${1}">${2}</p>',
     '<font ${2}>${3}</font>',
     '<span style="color:${2}">${3}</span>',
     '<img ${1} src="${2}"\>'
 );
 ​
 //开始替换
 $result=preg_replace($pattern, $replace,$string);
 echo $result;
 echo '<hr>'; 

注意: 在php7以后preg_replace()函数第一个参数正则中已经不支持e这个模式修正符号、也就是不再支持/e修饰符,如果要使用函数就请用: preg_replace_callback()函数

preg_replace_callback (参数..)函数 作用: 执行一个正则表达式搜索并且使用一个回调进行替换,匹配到的字符和子组都包含在回调函数的参数当中 参数列表: 参数1: 正则字符串 或一个数组 参数2: 处理替换的回调函数 支持 普通函数、匿名函数、类方法 参数3: 处理的字符串 参数4: 每个模式在每个字符串上进行替换几次数,默认是 -1 全部替换 参数5: 记录替换次数的引用变量 例1:匿名函数替换 代码案例如下

 $string='Assign up to ten teammates to an issue or pull request to make
 sure work has an owner. Mentioning other people or teams mysql
 in the issue will notify them if php something changes. javascript
 They can also stay in the php loop by opting to receive notifications whenever someone post';
 $pattern='/mysql|php|javascript/';
 echo preg_replace_callback($pattern, function($arr){
     //show($arr);
     echo $num++;
     return '<span style="color:red">'.strtolower($arr[0]).'</span>';
 }, $string); 

例2:类的方法替换方法 代码案例如下

$string='Assign up php to ten teammates to an issue or pull request to make
sure work has an owner. Mentioning other people or teams mysql
in the issue will notify them if php something changes. javascript
They can also stay in the php loop by opting to receive notifications whenever someone post';

$pattern=array(
    '/php/i',
    '/javascript/',
    '/mysql/'
);

class Replace{
    static public function txt($replace) {
        show($replace);
       if($replace[0]=='php'){
           return '<span style="color:red">'.$replace[0].'</span>';
       }
      return '<a href="" style="color:blue">'.$replace[0].'</a>';
    }
}

$result=preg_replace_callback($pattern, 'Replace::txt', $string);
echo $result; 

文章来源:智云一二三科技

文章标题:PHP正则表达式核心技术完全详解 第5节 php正则替换函数

文章地址:https://www.zhihuclub.com/76593.shtml

关于作者: 智云科技

热门文章

网站地图