ホームページ  >  記事  >  バックエンド開発  >  phpはエディターの画像にドメイン名を追加します

phpはエディターの画像にドメイン名を追加します

WBOY
WBOYオリジナル
2016-06-13 13:06:241312ブラウズ

php はエディターで画像にドメイン名を追加します

?

/**
 * 替换fckedit中的图片 添加域名
 * @param  string $content 要替换的内容
 * @param  string $strUrl 内容中图片要加的域名
 * @return string 
 * @eg 
 */
function replacePicUrl($content = null, $strUrl = null) {
	if ($strUrl) {
		//提取图片路径的src的正则表达式 并把结果存入$matches中  
    	preg_match_all("/<img(.*)src=\"([^\"]+)\"[^>]+>/isU",$content,$matches);
    	$img = "";  
        if(!empty($matches)) {  
        //注意,上面的正则表达式说明src的值是放在数组的第三个中  
        $img = $matches[2];  
        }else {  
           $img = "";  
        }
	      if (!empty($img)) {  
                $patterns= array();  
                $replacements = array();  
                foreach($img as $imgItem){  
	                $final_imgUrl = $strUrl.$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);
		
				return $vote_content;
		}else {
			return $content;
		}           		
	} else {
		return $content;
	}
}
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。