Question

I'm inside a foreach loop from a scandir() wheres the directory files are $files as $file. I'm trying to simplify my strripos filetype exclusions by using an array of needles, rather than passing several strripos lines for each filetype.

This works:

if ($code !== 'yes'){
    $excluded = strripos($file, '.js')
                || strripos($file, '.pl')
                || strripos($file, '.py')
                || strripos($file, '.py')
                || strripos($file, '.rb')
                || strripos($file, '.css')
                || strripos($file, '.php')
                || etc.;
    } else {
        $excluded = '';
    } 

But this does not:

    if ($code !== 'yes'){
        $exclusions = array('.js, .pl, .py, .rb, .css, .php, etc.');
        foreach($exclusions as $exclude){
            $excluded = strripos($file, $exclude);   
        } 
    } else {
        $excluded = '';
    } 

$code is a shortcode attribute defined by the user as 'yes' or anything else = no.

Then when I get to the output, I check if $excluded has been defined as 'yes.' Like I said, it works with the first example, but I can't get an array to work. To reiterate, I'm already inside the $file loop from the scandir().

UPDATE

Tried using in_array but I'm probably doing something wrong. I've tried:

$exclusions = array('.js', '.pl', '.py', '.rb', '.css', '.php', '.htm', '.cgi', '.asp', '.cfm', '.cpp', '.dat', '.yml', '.shtm', '.java', '.class');
$excluded = strripos($file, (in_array($exclusions)));

And I've tried:

$excluded = strripos($file, (in_array('.js', '.pl', '.py', '.rb', '.css', '.php', '.htm', '.cgi', '.asp', '.cfm', '.cpp', '.dat', '.yml', '.shtm', '.java', '.class')));

No go.

Was it helpful?

Solution

Your array currently has only one element, which is a long string:

'.js, .pl, .py, .rb, .css, .php, etc.'

You should quote each of your string elements like this:

$exclusions = array('.js', '.pl', '.py', '.rb', '.css', '.php', 'etc.');

Try changing your code to this:

$excluded = 'no';

if ($code !== 'yes'){
    $exclusions = array('.js', '.pl', '.py', '.rb', '.css', '.php'); 
    foreach($exclusions as $exclude){
        $check = strripos($file, $exclude); 
        if ($check !== false) {
            $excluded = 'yes';
            break;
        }
    } 
} 

Start by assigning $excluded = 'no';. As soon as strripos returns anything other than false you assign $excluded = 'yes'; and break out of the foreach loop. This way you end up with either 'yes' or 'no'.

OTHER TIPS

I assume you're trying to check if any of the files in a particular directory has an extension in your array $exclusions and if it does -- exclude that file.

So, if that's what you want, then you can create a function to make stripos accept arrays as needles:

function striposa($haystack, $needle, $offset=0) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
        if(stripos($haystack, $query, $offset) !== false) {
            return true; // stop on first true result
        }
    }
    return false;
}

(modified version of this answer)


And then, in your code, you can use it like below:

if ($code !== 'yes') {
    $exclusions = array('.js', '.pl', '.py', ...);
    $flag = striposa($file, $exclusions);

    if ($flag) {
        // file contains one of the extensions
    } else {
        // no matching extensions found
    }
}

Note that this will fail if the $file is something like hi.js.foo, but to make sure that doesn't happen, you can use pathinfo() to extract the extension as mentioned in this post.

Try the following:

$extensions = array('js', 'pl', ...);

$extension = strtolower(array_pop(explode('.', $file)));
$excluded = in_array($extension, $extensions);

if (! $excluded) {
  // do something with file
}

You could also use pathinfo to extract the extension.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top