blackhawk338
blackhawk338

Reputation: 386

PHP: Detect url in text, check if url is image or website and then echo image

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

Upvotes: 0

Views: 868

Answers (3)

blackhawk338
blackhawk338

Reputation: 386

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!

Upvotes: 0

Easy as pie

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

Upvotes: 0

DarkBee
DarkBee

Reputation: 15625

A function is a routine that returns a value that can be used in your program. Don't use functions to output stuf. Rewrite your function to :

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

Then use the function in your logic :

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

Upvotes: 2

Related Questions