Search code examples
phpglob

Exclude multiple keys from glob array PHP


I am new to php and i am trying to make a dinamic menu ( if i add a php file to the directory to be automaticaly added to the menu) my issue is that i want to exclude 3 specific pages from there like app.php, mdx.php and script.php this is the code i am using to get the menu up and running :

<?php
$dir = ".";
$htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);

// Sort in ascending order - this is default
echo '<ul>';
foreach($htmlFiles as $htmlFile)
{
  echo '<li><a href="'.basename($htmlFile).'">'.mb_strtoupper(basename($htmlFile,".php")).'</a></li>';

}
echo '</ul>';

?>

How do i do that ?


Solution

  • Maybe something like that?

    <?php
    $dir = ".";
    $cutFiles = ['app.php', 'mdx.php','script.php']; //file You don't want 
    $htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);
    
    $htmlFiles = array_diff($html_Files, $cutFiles); //computed differences
    
    // Sort in ascending order - this is default
    echo '<ul>';
    foreach($htmlFiles as $htmlFile)
    {
      echo '<li><a href="'.basename($htmlFile).'">'.mb_strtoupper(basename($htmlFile,".php")).'</a></li>';
    
    }
    echo '</ul>';
    
    ?>