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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路