<?PHP //snippet from Gobble Talk, a program that is made to chat to people like skype does, this function is made to convert links into media
if(isset($_GET['t']))
{
	if($_GET['t'] == "" || $_GET['t'] == NULL) { return print(""); }
	
	function URLconverter($content, $twitchsupport = 0)
    {
        $converted = '';

        $reg_Url = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
        $reg_Img = "/([^\"']?.*(png|jpg|jepg|gif))[\"']?/";
        $reg_Webm = "/([^\"']?.*(webm|gifv))[\"']?/";
        $reg_Mp4 = "/([^\"']?.*(mp4))[\"']?/"; //needs testing
        $reg_YT = "/([^\"']?.*(youtube))[\"']?/";
        $reg_Twitch = "/([^\"']?.*(twitch))[\"']?/";

        //dissolves entire text
        $ex_cont = explode(" ", $content);

        //runs through every dissolved part
        for($i = 0; $i < count($ex_cont); $i++)
        {
            //looks if dissolved part is an url
            if(preg_match($reg_Url, $ex_cont[$i], $url)) 
            {
                //converts it into a specific format
                if(preg_match($reg_Webm, $ex_cont[$i], $webm)){
                    $ex_cont[$i] = preg_replace($reg_Webm, '<a href="'.$url[0].'" target="_blank"><video controls><source src="'.$webm[0].'" type="video/webm"></video></a>', $ex_cont[$i]);
                
                }else if(preg_match($reg_YT, $ex_cont[$i], $yt)){
                    $yturl = explode("=", $url[2]);
                    $yturl = "https://www.youtube.com/embed/".$yturl[1];
                    $ex_cont[$i] = '<a href="'.$url[0].'" target="_blank"><iframe width="560" height="315" src="'.$yturl.'" frameborder="0" allowfullscreen></iframe></a>';
                
                }else if(preg_match($reg_Twitch, $ex_cont[$i], $twth) && $twitchsupport == 1){
                    $twitchurl = explode("/", $url[2]);
                    $twitchurl = "https://player.twitch.tv/?volume=0&channel=".$twitchurl[1];
                    $ex_cont[$i] = '<a href="'.$url[0].'" target="_blank"><iframe width="560" height="315" src="'.$twitchurl.'" frameborder="0" scrolling="no"></iframe></a>';
                
                }else if(preg_match($reg_Mp4, $ex_cont[$i], $mp4)){
                	$ex_cont[$i] = preg_replace($reg_Mp4, '<a href="'.$url[0].'" target="_blank"><video controls><source src="'.$mp4[0].'" type="video/mp4"></video></a>', $ex_cont[$i]);
                
                }else if(preg_match($reg_Img, $ex_cont[$i], $img)){
                    $ex_cont[$i] = preg_replace($reg_Img, '<a href="'.$url[0].'" target="_blank"><img src="'.$img[0].'"/></a>', $ex_cont[$i]);
                
                }else{
                    $ex_cont[$i] = preg_replace($reg_Url, '<a class="chatlink" href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $ex_cont[$i]);
                }
            }
        }

        //pastes everything back together
        for($i = 0; $i < count($ex_cont); $i++)
        {
            $converted .= $ex_cont[$i]." ";
        }

        //returns result
        return $converted;
    }

    //runs converter with Twitch embed support
    print(URLconverter($_GET['t'], 1));
}
?>