Reputation: 1398
I want to get an alternate of an sql query WHERE field LIKE '%subStr'
in PHP for example
actually I want to conditionally of different file types so I want to check whether the extension is .ppt
or .pptx
or .pptm
.ppm
or any thing else.
Upvotes: 0
Views: 82
Reputation: 3847
You could do it this way.
$file = 'my-file.ppt';
$parts = pathinfo($file);
$extensions = array("ppt", "pptx", "pptm", "ppm");
if (in_array($parts['extension'], $extensions)) {
echo "we have {$parts['extension']}";
}
else {
echo "no matches";
}
Upvotes: 1
Reputation: 73044
You can easily strip out the last characters of a filename string to achieve this:
<?php
$filename = "myFile.txt";
$pathinfo = pathinfo($filename);
$extension = $pathinfo['extension'];
switch ($extension) {
case "ppt":
echo "Extension is .ppt";
break;
case "txt":
echo "Extension is .txt";
break;
}
?>
Upvotes: 2
Reputation: 68556
Make use of array_pop
and explode
<?php
$filename='new.newfile.ppt';
$file=explode('.',$filename);
echo $ext = array_pop($file); // ppt
Upvotes: 2