PHP制作可点击链接

时间:2015-01-05 10:44:20

标签: php html html-entities

我这里有一个创建可点击链接的功能:

function makeClickableLinks($text) {
        $notIncludedonLink = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $text);  // removing not included on the link
        $urlLink = str_replace($notIncludedonLink,'',$text);
        $finalText = str_replace($urlLink,'<a href="'.$urlLink.'" target="_blank">'.$urlLink.'</a>',$text);
        return $finalText;
    }

但不是返回普通的可点击链接:

http://docs.google.com/

显示:

<a href="http://docs.google.com/" target="_blank">http://docs.google.com/</a>

我尝试使用htmlentities,但它不起作用。

这是一个将数据发送到服务器的JS代码:

function checkNewLink() {
var latestId = $("input[name=latestLink]").val();
$('.newReply').load("links/ajax.php?action=newreply&msgid=<?php echo $msgId; ?>&latestid=" + latestId);
}
setInterval("checkNewLink()", 200);

其中latestId包含输入的链接。它将被发送到ajax.php。每隔200毫秒,它将检查是否有新的输入链接。

1 个答案:

答案 0 :(得分:2)

<?php
function makeClickableLinks($text){
    return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $text);
}

echo makeClickableLinks('test http://docs.google.com/ test');

输出代码(http://codepad.org/EZE1HFZ4

  

测试http://docs.google.com/测试

更新后

变化

setInterval("checkNewLink()", 200);

setInterval(function(){ checkNewLink() }, 200);

阅读setInterval()方法

相关问题