Extract URL from string

前端 未结 5 642
野的像风
野的像风 2020-11-27 06:41

I\'m trying to find a reliable solution to extract a url from a string of characters. I have a site where users answer questions and in the source box, where they enter thei

5条回答
  •  迷失自我
    2020-11-27 06:54

    This code is worked for me.

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

提交回复
热议问题