首頁  >  文章  >  後端開發  >  php查找、取代字串中http位址的程式碼分享

php查找、取代字串中http位址的程式碼分享

WBOY
WBOY原創
2016-07-25 08:57:241357瀏覽
本文介绍下,用php在指定字符串中查找http地址,并进行替换的一例代码,有需要的朋友参考下。

在字符串中查找、替换http地址,代码如下:

<?php
/**
* 查找、替换字符串http地址
* edit by bbs.it-home.org
*/
function convertImg($str)  
{  
   $str = preg_replace("/\{([^}]+)\}/",'<div style="border:1px dashed #003366; 
background-color:#f0f0f0; height:21px; color:#003399">回复:$1</div>',preg_replace("/\[(\d+)\]/",
'<img  src="/images/face/face$1.jpg"/ alt="php查找、取代字串中http位址的程式碼分享" >',$str));  
   $reg="/http\:\/\/(\w+\.)+(net|com|org|cn|kr|jp|tw)[A-Za-z0-9_&\/\?=]*/i";  
   if(preg_match_all($reg,$str,$out))  
   {  
   for($i=0;$i<count($out[0]);$i++)  
   {  
     $link=substr(base64_encode(md5(preg_replace("/http\:\/\//","",$out[0][$i]))),0,10+strlen($str)%10);  
     $str=str_replace($out[0][$i],'http://'.$link.'',$str);
}  
}  
return $str;  
}
?>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn