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

时间:2014-08-17 10:45:04

标签: javascript php html image url

所以我正在研究一个项目,我需要做的是我有一些文本,在那个文本中它有很多单词,然后是一个url,这是一个图像。首先我需要做的是检测该网址是否是网站或图像,然后如果是图像我需要使用<img>标签显示图像,如果是网站则回显网址<a href>标签。到目前为止,我有一个脚本来检测它是否是网址或图像,但我仍然需要回显文本中的图像或网址。这是脚本:

<?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 个答案:

答案 0 :(得分:1)

函数是一个返回可在程序中使用的值的例程。 不要使用函数输出stuf。将您的功能重写为:

<?php
function isValidImage($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; // Its an image
        }
        return false;// Its an URL
    }
}

然后使用逻辑中的函数:

<?php
$urls = [
    'http://www.google.be',
    'http://hearstcommerce.ca/customcontent/members/premium/sample.jpg',
];

foreach($urls as $url) {
   if (isValidImage($url) {
      echo '<img src="'.$url.'" />';
   }else{
      echo '<a href="'.$url.'">'.$url.'</a>';
   }
}

答案 1 :(得分:0)

简单就是馅饼

if(isset($valid_image_type[$type])){
    $ech = '<img src="'.$url.'"/>';  
} else {
    $ech = '<a href=".'$url'.">".'$url'."<a>';
}
echo $ech;

答案 2 :(得分:0)

好的,所以我设法解决了,我的解决方案是

       ?>    
       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;
    }
?>

随意使用这个人!