dopr25398 2014-08-17 10:45
浏览 42
已采纳

PHP:检测文本中的URL,检查url是图像还是网站,然后回显图像

So I'm working on a project, and what I need to do is I have some text, and in that text it has lots of words and then a url which is an image. What I need to do it first, detect if that url is a website or an image, and then if it is an image I need to display the image with the <img> tags, and if it's a website echo the url with the <a href> tags. So far I have a script to detect if it's a url or image, but I still need to echo the image or url in the text. Here is the script:

<?php
function detectImage($url) {
    $url_headers=get_headers($url, 1);
    if(isset($url_headers['Content-Type'])){
        $type=strtolower($url_headers['Content-Type']);
        $valid_image_type=array();
        $valid_image_type['image/png']='';
        $valid_image_type['image/jpg']='';
        $valid_image_type['image/jpeg']='';
        $valid_image_type['image/jpe']='';
        $valid_image_type['image/gif']='';
        $valid_image_type['image/tif']='';
        $valid_image_type['image/tiff']='';
        $valid_image_type['image/svg']='';
        $valid_image_type['image/ico']='';
        $valid_image_type['image/icon']='';
        $valid_image_type['image/x-icon']='';
        if(isset($valid_image_type[$type])){
            echo "url is image";
        } else {
            echo "url is website";
        }
    }
}
?>
  • 写回答

3条回答 默认 最新

  • duanqinqiao4844 2014-08-17 11:14
    关注

    Ok so I managed to solve it, my solution was

           ?>    
           function detectImage($url) {
            $url_headers=get_headers($url, 1);
            if(isset($url_headers['Content-Type'])){
                $type=strtolower($url_headers['Content-Type']);
                $valid_image_type=array();
                $valid_image_type['image/png']='';
                $valid_image_type['image/jpg']='';
                $valid_image_type['image/jpeg']='';
                $valid_image_type['image/jpe']='';
                $valid_image_type['image/gif']='';
                $valid_image_type['image/tif']='';
                $valid_image_type['image/tiff']='';
                $valid_image_type['image/svg']='';
                $valid_image_type['image/ico']='';
                $valid_image_type['image/icon']='';
                $valid_image_type['image/x-icon']='';
                if(isset($valid_image_type[$type])){
                    return true;
                } else {
                    return false;
                }
            }
        }
        function detectLink($string) {
            $content_array = explode(" ", $string);
            $output = '';
            foreach($content_array as $content) {
                if(substr($content, 0, 7) == "http://" || substr($content, 0, 4) == "www.") {
                    if (detectImage($content)===true) {
                        $content = '<img src="'.$content.'">';
                    } else {
                        $content = '<a href="'.$content.'">'.$content.'</a>';
                    }
                }
                $output .= " " . $content;
            }
            $output = trim($output);
            return $output;
        }
    ?>
    

    Feel free to use this anyone!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?