简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM