0

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 Answers 3

2

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>';
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Easy as pie

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

2 Comments

But then, what if you also have text in the string like "hellp image.image" the script only works for images, won't work for text.
So first you have to find the http:// in the string and from there to the first " " space are the urls that are you looking for, Then you use your function
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.