PHP制作可点击的链接


PHP making clickable links

我这里有一个创建可点击链接的函数:

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 毫秒,它将检查是否有新的输入链接。

<?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);

read setInterval() 方法