【问题标题】:how to skip link from function being convert href into another link如何从将href转换为另一个链接的函数中跳过链接
【发布时间】:2013-08-04 15:58:06
【问题描述】:

下面是我从 url 转换为链接的函数:

function url_to_link($string){

/*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

/*** make all URLs links ***/
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

/*** make all emails hot links ***/
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);

return $string;
}

function update_todo() {

    //var input
    $projects_id = $_POST['projects_id'];
    $content = url_to_link($_POST['content']);  // <----- url_to_link()
    $date_created = date('Y-m-d H:i:s');


    $sth = $this->db->prepare('SELECT * FROM `doc_to_do` where projects_id="'.$projects_id.'" ');
    $sth->execute();
    $rows = $sth->fetchAll();
    $total_rows = count($rows);

    if ($total_rows > 0) {
        //update    
        $sql = "UPDATE `doc_to_do` SET content=?  WHERE projects_id=?";
        $sth = $this->db->prepare($sql);
        $sth->execute(array($content,$projects_id));
    }
    else {
        //insert
        $sth = $this->db->prepare('INSERT INTO `doc_to_do` (projects_id, content, date_created) VALUES (:projects_id, :content, :date_created)');
        $sth->execute(array(':projects_id' => $projects_id, ':content' => $content, ':date_created' => $date_created));
    }
}

我尝试使用上面的函数将我的具有 url 的内容转换为链接。首次保存链接时如下所示:

<p><a target="_blank" href="http://stackoverflow.com/">http://stackoverflow.com/</a></p>

当我再次尝试保存时,链接将断开:

<p><a target=\"_blank\" href=\"<a target="_blank" href="http://stackoverflow.com/">http://stackoverflow.com/</a>\"><a target="_blank" href="http://stackoverflow.com/">http://stackoverflow.com/</a></a></p>

你可以看到我的功能不够好,它应该忽略链接但只转换网址

【问题讨论】:

标签: php


【解决方案1】:

我只是先从我的内容中删除链接,然后创建新的:

function url_to_link($string){

// remove all href to prevent duplicate(when convert) then create new one
$string = strip_tags($string, '<p><b><i><s><ul><ol><li><strong>');

/*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

/*** make all URLs links ***/
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$string);

/*** make all emails hot links ***/
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$string);

return $string;
}

【讨论】:

    【解决方案2】:

    在将文本放入数据库之前,不要每次都通过 url_to_link 函数运行文本。将用户输入的原始非 HTML 数据存储在数据库中,并且仅在网站上显示 URL 时将其转换为 HTML 链接。所以只有:

    echo url_to_link($contentFromDatabase);
    

    将未处理的原始文本存储在数据库中并尽可能晚地处理它总是一个好主意。一方面,HTML 链接仅特定于 HTML。假设您想在稍后的某个时间点以纯文本电子邮件或其他非 HTML 格式发送文本;如果你只有处理过的文本,你就有麻烦了。

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 2018-08-17
      • 2015-10-23
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多