Input of email and url to be clickable

前端 未结 2 1911
情歌与酒
情歌与酒 2021-01-24 09:07

I have a form that I process in PHP. Users sometimes put their email address in the form or URLs. These usually come out as text after I strip the input of tags.

Recen

相关标签:
2条回答
  • 2021-01-24 09:50

    you have to wrap the url in a anchor tag:

    assuming $myLink is the link text coming from your db:

    <a href="<?php echo $myLink; ?>"><?php echo $myLink; ?></a>
    
    0 讨论(0)
  • 2021-01-24 09:59

    You can use a regular expression based function like this

    function  autolink($message) { 
        //Convert all urls to links
        $message = preg_replace('#([\s|^])(www)#i', '$1http://$2', $message);
        $pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
        $replacement = '<a href="$1" target="_blank">$1</a>';
        $message = preg_replace($pattern, $replacement, $message);
    
        /* Convert all E-mail matches to appropriate HTML links */
        $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
        $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
        $replacement = '<a href="mailto:\\1">\\1</a>';
        $message = preg_replace($pattern, $replacement, $message);
        return $message;
    
    0 讨论(0)
提交回复
热议问题