當前位置:首頁 » 編程語言 » php正則匹配替換

php正則匹配替換

發布時間: 2022-06-20 17:33:17

php正則表達式如何替換掉某字

<?php

$string='倚天屠龍記2019版粵語版';

$pattern='/版$/';

$replacement='';

echopreg_replace($pattern,$replacement,$string);

?>

$pattern='/版$/';正則表達式,匹配最後一個字是版.匹配第一個'版'。'/版/'。匹配所有的版'/版/g'。

$replacement='';用來替換的內容,去掉就是替換為空字元串。

更多需求查看php正則表達式教程網頁鏈接

㈡ PHP正則替換

回答者「gpgkd906」的方法很好,學習了,我也把較初級的辦法貼一下,供大家參考:
<?php
$content='<img style="cursor: pointer" border="0" alt="復地紫城外景" width="550" height="437" onclick="window.open(\'/uploads/allimg/110504/172_110504113610_1.jpg\')" src="/uploads/allimg/110504/172_110504113610_1.jpg" />';

//php中(\S+)要補上一個加號變成兩個,即(\S++)
$s2=preg_replace("/(<img[^>]+(?:src=))(\S++)([^>]*>)/iU","<a href=$2>$1 $2 $3</a>",$content);
echo $s2."\r\n\r\n\r\n"; ///輸出第一遍出理結果

//1、如果IMG中有ALT標記,則自動給A鏈接里加一個TITLE標記,其內容用ALT的。
$s2=preg_replace("/(<a[^>]+)(><img[^>]+?)alt=(\S++)([^>]+>)/iU","$1 title=$3$2$4",$s2);

//2、去掉IMG里的onclick標記
$s2=preg_replace("/(<a[^>]+)(><img[^>]+?)(onclick=\S++)([^>]+>)/iU","$1$2$4",$s2);
echo $s2."\r\n\r\n";
?>

㈢ PHP 正則表達式替換字元

$str="{235大吉大利abc}";
$str=preg_replace("/[a-z,A-Z,0-9]/","*",$str);//替換所有字母和數字為*
echo$str;

㈣ PHP正則表達式如何替換像這樣的案例的

這個用正則表達式替換是比較簡單的,因為有分隔符/,所以把分隔符後面的都替換掉就行了.

<?php

$str="龍門飛甲/龍門飛甲電視劇版";

$regex="~/.*?$~";

echo preg_replace($regex,"",$str);

?>

㈤ PHP如何利用正則表達式匹配替換以下類型

按照你的要求編寫的把除字母外的其他空格替換成逗號的PHP程序如下

<?php

$str="李連傑JetLi關之琳Marc-AndréGrondin莫妮亞·肖克里CharlotteSt-MartinBBCCDD";

$newstr=preg_replace('/([x{4e00}-x{9fa5}a-z]+)s+([p{L}p{So}-]+s+[p{L}p{So}-]+)s+/iu','$1,$2,',$str);

$newstr=preg_replace('/([x{4e00}-x{9fa5}a-z]+)s+([p{L}p{So}-]+s+[p{L}p{So}-]+)$/iu','$1,$2',$newstr);

echo$newstr;

?>

運行結果

李連傑,Jet Li,關之琳,Marc-André Grondin,莫妮亞·肖克里,Charlotte St-Martin,BB,CC DD

㈥ php 正則匹配替換

$str='[font=宋體][size=2]最難忘的中秋故事是大家一起吃月餅、田螺、栗子、菱角、芋頭、西瓜、蜜瓜、柚子、鳳爪、糖水等等,應有盡有![/size][/font]';
$str2='[i=s]本帖最後由xie於2014-8-1200:06編輯[/i]';
$str3='[attach]146[/attach]';
echopreg_replace('/[(.*?)]/is','',$str)." ";
echopreg_replace('/[(.*?)]/is','',$str2)." ";
echopreg_replace('/[(.*?)]/is','',$str3)." ";

㈦ php 如何正則替換

很簡單,代碼如下(其實不用正則也可以,strstr()與str_replace()函數也能替換):

// 需要替換的字元串
$string = 'D:\wwwroot\cms\index.php'; // 假設一個路徑
// 正則樣式
$pattern = '/\\/';
// 檢測是否需要替換
if (preg_match($pattern, $string)) {
// 開始替換\為/
$string = preg_replace($pattern, '/', $string);
}
// 輸出替換後的字元串
echo $string; // D:/wwwroot/cms/index.php

㈧ php正則替換怎麼寫

正則表達式:id="[^"]*"

完整的php程序如下:

<?php
$str='<divid="yui131432432">問額外額外</div><divid="yui136732432">問額外額外</div><divid="6346">問額外額外</div><divid="3232">問額外額外</div><div
id="25346463">問額外額外</div>';
echopreg_replace('#id="[^"]*"#i','',$str);
?>

運行結果:

<div >問額外額外</div><div >問額外額外</div><div >問額外額外</div><div >問額外額外</div><div >問額外額外</div>

㈨ PHP正則函數替換問題

php正則表達式是貪婪匹配的,所以全部匹配了,
替換uid=1233:$msg = preg_replace("/uid=.+?\&/is", "", $msg);
替換&id=123&:$msg = preg_replace("/\&id=.+\&/is", "", $msg);
替換&id=123:$msg = preg_replace("/\&id=.+?/is", "", $msg);

㈩ php如何進行正則替換

按照你的要求把h後的數字和w後的任意數字替換成固定數的php程序如下

<?php

$fix='555';//固定數

$str='asdasda/w/100/h/200/q/sdasdsad';

$regex1="~h/[0-9]+~";

$result=preg_replace($regex1,"h/".$fix,$str);

$regex2="~w/[0-9]+~";

$result=preg_replace($regex2,"w/".$fix,$result);

print_r($result);

?>
運行結果

asdasda/w/555/h/555/q/sdasdsad

熱點內容
java軟體免費下載 發布:2025-03-20 10:26:01 瀏覽:705
安卓用什麼編譯 發布:2025-03-20 10:25:57 瀏覽:808
ftp中文軟體下載 發布:2025-03-20 10:07:47 瀏覽:508
nexus7android 發布:2025-03-20 10:06:58 瀏覽:619
安舍iq8如何修改密碼 發布:2025-03-20 10:06:17 瀏覽:880
解壓RTP 發布:2025-03-20 09:59:37 瀏覽:161
python量化分析 發布:2025-03-20 09:53:05 瀏覽:626
手機熱點有限的訪問許可權 發布:2025-03-20 09:50:46 瀏覽:440
為什麼安卓沒有ios系統流暢 發布:2025-03-20 09:50:43 瀏覽:793
python編程實例 發布:2025-03-20 09:48:19 瀏覽:294