Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to determine if string is image, youtube, etc

So, I'm looking to code in PHP a function, getType(). This function will take a user inputed string from a web form (in CodeIgniter) and then from there analyze it's content and then determine if the string is a photo (ending in .jpg, .png, etc), a youtube video link, a vimeo video link, or just text.

I'm just having a hard time trying to visualize the best, most economical way to do this.

if (strpos($content, ".jpg|.png|.bmp"))
{ return "image"; }
else if (strpos($content, "youtube.com"))
{ return "youtube"; }
else if (strpos($content, "vimeo.com"))
{ return "vimeo" }
else
{ return "text" }
like image 748
bswinnerton Avatar asked Dec 11 '25 17:12

bswinnerton


1 Answers

This should work:

// check if string ends with image extension
if (preg_match('/(\.jpg|\.png|\.bmp)$/', $content)) {
    return "image";
// check if there is youtube.com in string
} elseif (strpos($content, "youtube.com") !== false) {
    return "youtube";
// check if there is vimeo.com in string
} elseif (strpos($content, "vimeo.com") !== false) {
    return "vimeo";
} else {
    return "text";
}

Demo: http://codepad.viper-7.com/1V4joK

Note that there is no guarantee that it is a youtube or vimeo link. Because this only checks whether the string matches the service and nothing more.

like image 55
PeeHaa Avatar answered Dec 16 '25 04:12

PeeHaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!