⑴ 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 如何正則替換
很簡單,代碼如下(其實不用正則也可以,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正則替換函數 preg_replace ,替換部分字元
只想將 $param 替換為 $pstr的話就不要用正則。
$phpstr = str_replace('$param','$pstr',$phpstr);
⑷ php指定范圍批量正則匹配與替換
preg_replace(array('/>小明/'),array('>小王'),html代碼);//已經過測試有效
⑸ php 正則表達式替換
<?php
@header('Content-type:text/html;charset=GBK');
$subject='<imgtitle="美國留學網站"alt="美國留學網站">美國留學網站<imgtitle="美國留學網站"alt="美國留學網站">';
$pattern="/(留學)(?![^'"><]*['"])/";
$result=preg_replace($pattern,"<a>$1</a>",$subject);
echo$result;
?>
⑹ php正則表達式替換問題php函數preg_replace的使用問題
都幫你測試好了,正則多寫一個<ul>就能搞定了:
<?php
$str = '你的一大堆字元串'; // 你如果直接放字元串,需要用單引號
$reg = '/<div class="pic-list">\s+<ul>.*?<\/ul>\s+<\/div>/is';
$new_str = preg_replace( $reg, '', $str);
echo $new_str;
?>
⑺ php正則替換
如果你是想要把空格前後的內容換換位置的話,是完全可以的。
首先以空格為分隔,把字元串變成數組.$arr_titles = explode(' ',$titles);這樣就得到一個數組,$arr_titles的內容應該是('手提包','超級甜美味兒簡約蝴蝶結箱型手提包'),你可以用print_r($titles)打出來看一下。
然後把數組$arr_titles反過來排序。
$i = count($arr_titles);//獲取數組長度
$new_arr_titles = array();//定義新數組
foreach($arr_titles as $k=>$v){
$new_arr_titles[$i-$k] = $v;
}
然後拼接回數組。$new_titles = implode(' ',$arr_titles);
這樣不管$title中有多少個按照空格分隔的字元串,都可以反過來排序了。
不知道是不是你想要的效果,如果不是可以追問,我繼續回答。如果可以幫到你,希望你能夠採納。謝謝。
⑻ PHP正則表達式替換函數eregi_replace不起作用
在5.3版的以後的php,比如5.4, 5.5,已經廢除了eregi_replace函數
去掉@,用preg_replace替代。
⑼ 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
⑽ php 正則函數 preg_replace
因為數組裡面你先寫了#ab#,後寫的#abc#,實際工作時系統先使用第一組參數替換,然後使用第二組參數替換。
如果你希望得到22結果,應該交換數組裡面兩個的順序。