PHP正則表達(dá)式核心技術(shù)完全詳解 第5節(jié) php正則替換函數(shù)

極客小俊
一個(gè)專注于web技術(shù)的80后
你不用拼過聰明人,你只需要拼過那些懶人 你就一定會(huì)超越大部分人!

說到替換呢 這里我覺得首先要回憶一下字符串的相關(guān)替換函數(shù)!
str_replace() ? 在字符串中查找字符,然后替換成想要的字符 str_ireplace() ?在字符串中查找字符,然后替換成想要的字符 (不區(qū)分大小寫) 小提示 str_replace() 函數(shù)是全局替換
練習(xí): 在字符串中查找數(shù)組對(duì)應(yīng)的字符,設(shè)為紅色 代碼如下:
?//在字符串中查找數(shù)組對(duì)應(yīng)的字符,設(shè)為紅色
?
?$string='[北京市朝陽區(qū)HTTP://www.baidu.com
? ? ?天安門北京市朝陽區(qū)http://www.sina.com天安門
? ? ?北京市朝陽區(qū)HTTP://www.163.com天安門]';
?
?$data=[
? ? ?'http://www.baidu.com',
? ? ?'http://www.sina.com',
? ? ?'http://www.163.com'
?];
?
?//處理之前
?echo nl2br($string);
?
?echo '<hr>';
?
?foreach ($data as $k=>$v){
? ? ?//這里使用的是不區(qū)分大小寫的str_ireplace()方法來替換
? ? ?$string=str_ireplace($v, '<span style="color:red;">'.$v.'</span>', $string);
?}
?
?//處理之后
?echo nl2br($string);
?
str_replace()方法中的參數(shù) 有三種使用技巧 第一種: str_replace(string,string,string,替換次數(shù)); 代碼如下:
?$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 '替換的次數(shù)是:'.$count.'次!';
第二種: str_replace(array,string,string,替換次數(shù)); 粗暴點(diǎn) , 代碼如下:
?$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!";
?//查找的數(shù)據(jù)
?$search=array(
? ? ?'美國',
? ? ?'日本',
? ? ?'韓國',
? ? ?'法國',
? ? ?'澳大利亞'
?);
?//替換為
?$replace='<span style="color:#ff0000;">***</span>';
?
?//開始替換
?echo str_replace($search, $replace, $string,$count);
?echo '<br>';
?echo '替換的次數(shù)是:'.$count.'次!';
第三種: str_replace(array,array,string,替換次數(shù)); 代碼如下:
?$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!";
?
?//查找的數(shù)據(jù)
?$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 '替換的次數(shù)是:'.$count.'次!';
以上就是回顧了一下 字符串中的替換,別走開,接下來我們才要進(jìn)入正則函數(shù)替換的正題哦!
PHP正則替換函數(shù) preg_replace(); ?正則中替換函數(shù)、返回值可能是一個(gè)字符串也可能是一個(gè)數(shù)組
正常使用 preg_replace(參數(shù)..) 參數(shù)列表: 參數(shù)1: 正則字符串或者正則數(shù)組參數(shù)replacement替換為的字符串 或者 字符串?dāng)?shù)組 參數(shù)3: $string處理的字符串 或者 字符串?dāng)?shù)組 參數(shù)4: 每個(gè)模式在每個(gè)字符串上進(jìn)行替換幾次數(shù),默認(rèn)是 -1 全部替換 參數(shù)5: 記錄替換次數(shù)的引用變量 代碼案例如下:
?$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!";
?//查找的數(shù)據(jù)
?$search='/php | mysql | javascript/';
?//替換為
?$replace='<font color="red">***</font>';
?//開始替換
?echo preg_replace($search, $replace,$string);
使用子模式進(jìn)行替換 ? ?子模式可以用到第二個(gè)參數(shù)當(dāng)中 代碼案例如下:
?$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!";
?//查找的數(shù)據(jù)
?$search='/(php | mysql | javascript)/';
?//替換為 這里要記得字符串的單雙引號(hào)對(duì)反向引用的作用哦! 前面提到過 !!不懂的同學(xué)可以回去看看!!
?$replace="<font color='red'>\\1</font>";
?//開始替換
?echo preg_replace($search, $replace,$string);
在前兩個(gè)參數(shù)中都使用數(shù)組,可以一起將多個(gè)模式(正則)同時(shí)替換對(duì)應(yīng)的多個(gè)值 替換UBB代碼, 這里就不介紹UBB是什么了 自己百度一下就會(huì)知道簡單地一匹 代碼案例圖如下:
?$string="online [align=left][b]PHP開發(fā)[/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 '<hr>';
?
?//查找的數(shù)據(jù)
?$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()函數(shù)第一個(gè)參數(shù)正則中已經(jīng)不支持e這個(gè)模式修正符號(hào)、也就是不再支持/e修飾符,如果要使用函數(shù)就請(qǐng)用: preg_replace_callback()函數(shù)
preg_replace_callback(參數(shù)..)函數(shù) 作用: 執(zhí)行一個(gè)正則表達(dá)式搜索并且使用一個(gè)回調(diào)進(jìn)行替換,匹配到的字符和子組都包含在回調(diào)函數(shù)的參數(shù)當(dāng)中 參數(shù)列表: 參數(shù)1: 正則字符串 或一個(gè)數(shù)組 參數(shù)2: 處理替換的回調(diào)函數(shù) 支持 普通函數(shù)、匿名函數(shù)、類方法 參數(shù)3: 處理的字符串 參數(shù)4: 每個(gè)模式在每個(gè)字符串上進(jìn)行替換幾次數(shù),默認(rèn)是 -1 全部替換 參數(shù)5: 記錄替換次數(shù)的引用變量 例1:匿名函數(shù)替換 ? 代碼案例如下
?$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;

如果喜歡話請(qǐng) 點(diǎn)贊 ?投幣 ?收藏 一鍵三連 ?
大家的支持就是我堅(jiān)持下去的動(dòng)力!
?B站技術(shù)交流群:855256321
不要忘了?? 關(guān)注 ??哦!
