上午闲来无事 写了一个获取HTTP://协议下所有格式URL的正则表达式(点击查看):但是代码有个BUG 就是 当有相同域名的时候 替换会出问题
下面是修改后代码
- <?php
- $newContent = "我在永余PHP技术社区发现了好多高级教程,快来看呀!91php.com www.91php.com http://www.91php.com/shipin.html http://www.91php.com/lamp.html http://www.91php.com/jscss.html";
- $pattern = "/(http:\/\/)*(www)*[\w-]*(\.)*[\w-]+\.(com|net|org|gov|cc|fm|me|biz|info|cn)(\.(cn|hk))*(\/)*([\w-\]\/\.\?\=\&])*/";
- preg_match_all($pattern, $newContent, $matches);
- for($i=0;$i<count($matches[0]);$i++){
- if(strpos($matches[0][$i],"http://")!==false){
- $cleanURL = str_replace("http://","",$matches[0][$i]);
- $newContent = str_replace($matches[0][$i],$cleanURL, $newContent);
- }
- }
- $newContent = preg_replace($pattern,"<a href=\"http://$0\" target='_blank'>http://$0</a><br>", $newContent);
- echo $newContent;
- ?>
<?php
$newContent = "我在永余PHP技术社区发现了好多高级教程,快来看呀!91php.com www.91php.com http://www.91php.com/shipin.html http://www.91php.com/lamp.html http://www.91php.com/jscss.html";
$pattern = "/(http:\/\/)*(www)*[\w-]*(\.)*[\w-]+\.(com|net|org|gov|cc|fm|me|biz|info|cn)(\.(cn|hk))*(\/)*([\w-\]\/\.\?\=\&])*/";
preg_match_all($pattern, $newContent, $matches);
for($i=0;$i<count($matches[0]);$i++){
if(strpos($matches[0][$i],"http://")!==false){
$cleanURL = str_replace("http://","",$matches[0][$i]);
$newContent = str_replace($matches[0][$i],$cleanURL, $newContent);
}
}
$newContent = preg_replace($pattern,"<a href=\"http://$0\" target='_blank'>http://$0</a><br>", $newContent);
echo $newContent;
?>
欢迎所有朋友发表建议及意见,欢迎高手发表更有效地方法! My QQ : 442206