php文本替换-完整解读PHP正则表达式核心技术第五节 PHP正则替换函数

专注网络技术的90后

你不必和聪明人斗,你只要和这些懒人斗,你一定能赶上大多数人!

说到替换,我想我们首先要回忆一下字符串的相关替换函数!

str_replace() 查找字符串中的字符并将其替换为所需的字符 str_ireplace() 查找字符串中的字符并将其替换为所需的字符(不区分大小写) 提示 str_replace() 函数是全局替换

替换文本怎么操作_替换文本中的文字_php文本替换

练习:找到字符串中该字段对应的字符,并将其设置为黑码php文本替换,如下:

 //在字符串中查找数组对应的字符,设为红色
 
 $string='[北京市朝阳区HTTP://www.baidu.com
     天安门北京市朝阳区http://www.sina.com天安门
     北京市朝阳区HTTP://www.163.com天安门]';
 ​
 $data=[
     'http://www.baidu.com',
     'http://www.sina.com',
     'http://www.163.com'
 ];
 ​
 //处理之前
 echo nl2br($string);
 ​
 echo '
';  ​  foreach ($data as $k=>$v){      //这里使用的是不区分大小写的str_ireplace()方法来替换      $string=str_ireplace($v, ''.$v.'', $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='php';
 echo str_replace($search, $replace, $string,$count);
 echo '
';  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='***';
 ​
 //开始替换
 echo str_replace($search, $replace, $string,$count);
 echo '
';  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=[
     '***',
     '???',
     '###',
     '@@@',
     '&&&',
 ];
 ​
 //开始替换
 echo str_replace($search, $replace, $string,$count);
 echo '
';  echo '替换的次数是:'.$count.'次!';

以上是对字符串替换的回顾。 别走开。 接下来我们就进入正则函数替换的题外话!

PHP正则替换函数preg_replace(); 正则替换函数的返回值可以是字符串,也可以是字段。

正常使用 preg_replace(parameter..) 参数列表: 参数1:正则字符串或正则字段参数 替换为字符串或字符串字段 参数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='***';
 //开始替换
 echo preg_replace($search, $replace,$string);

使用子模式替换子模式可以使用第二个参数。 代码示例如下:

 $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="\1";
 //开始替换
 echo preg_replace($search, $replace,$string);

前两个参数使用链表,可以同时将多个模式(规律性)替换为对应的多个值来替换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]http://localhost/test3/1.jpg[/img]";
 ​
 //替换之前
 echo $string;
 echo '
';  ​  //查找的数据  $pattern=array(      '/[u](.+)[/u]/',      '/[b](.+)[/b]/',      '/[align=(left|center|right)](.+)[/align]/',      '/[font(s+)?(size=d)?](.+)[/font]/',      '/[color(s+)?color=([a-zA-Z]+)?](.+?)[/color]/',      '/[imgs(width=d{1,3})?](.+)[/img]/'  );  //替换为  $replace=array(      '${1}',      '${1}',      '

${2}

',      '${3}',      '${3}',      ''  );  ​  //开始替换  $result=preg_replace($pattern, $replace,$string);  echo $result;  echo '
';

注意:php7之后,preg_replace()函数的第一个参数不支持e模式修正符号,即不再支持/e修饰符。 如果要使用该函数,请使用:preg_replace_callback()函数

preg_replace_callback(parameter..) 功能:执行正则表达式搜索并使用反弹替换,匹配到的字符和子组包含在回调函数的参数中 参数列表: 参数 1:正则字符串或字段 参数 2:回调处理替换的函数支持普通函数、匿名函数和类方法 参数 3:要处理的字符串 参数 4:每个字符串上每个模式被替换的次数,默认为 -1 times 参考变量 示例 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 ''.strtolower($arr[0]).'';
 }, $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 ''.$replace[0].'';
       }
      return ''.$replace[0].'';
    }
}
$result=preg_replace_callback($pattern, 'Replace::txt', $string);
echo $result;

今日头条@吉客小俊php文本替换,第一篇原创文章