Search code examples
phpglobspaces

remove spaces with listing folders using glob()


I tried to list folders of a directory with the glob() function of PHP. I also used str_replace.

This is my code -

<?php
/*      files directory (strukture):
 *files
 *  folder_01
 *  folder_02
 *      test.png
 *      test.txt
 *  folder_03
*/


$file_dir = "files";
foreach(glob($file_dir.'/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace($file_dir.'/','',$dir);
    echo basename($dir).PHP_EOL./*why is between the basename and the following text a space?*/">>some text<<"."<br>";
}

?>

Now I get spaces after the basename. I don't want the spaces and need help with trying to remove the spaces in basename.


Solution

  • That's because of your PHP_EOL. When it's rendered as HTML it becomes what's known as a whitespace which will simply appear as a space. Remove it and you'll be happy.