//要替換的內容 $content = '<img alt="" src="js/fckeditor/UserFiles/image/F201005201210502415831196.jpg" width="600" height="366"><br><br><br><br><img alt="" src="js/fckeditor/UserFiles/image/33_avatar_middle.jpg" width="120" height="120">'; //提取圖片路徑的src的Regex preg_match_all("/<img(.*)src="([^"]+)"[^>]+>/isU",$content,$matches); $img = ""; if(!empty($matches)) { //注意,上面的Regex說明src的值是放在數組的第三個中 $img = $matches[2]; }else { $img = ""; } if (!empty($img)) { $img_url = "http://".$_SERVER['SERVER_NAME']; $patterns= array(); $replacements = array(); foreach($img as $imgItem){ $final_imgUrl = $img_url.$imgItem; $replacements[] = $final_imgUrl; $img_new = "/".preg_replace("///i","/",$imgItem)."/"; $patterns[] = $img_new; } //讓數組按照key來排序 ksort($patterns); ksort($replacements); //替換內容 $vote_content = preg_replace($patterns, $replacements, $content); |