Jump to content

Question reguarding echo str_replace()


al3x8730

Recommended Posts

Actually I'm trying to replace something in a directory contents listing.

 

I want to replace 3 file names.

 

And for 1 I'm using this:

<?php

$handle=opendir(".");

while (($file = readdir($handle))!==false) {
echo str_replace( "index.php", "", $file ) . " <br />";

}
closedir($handle);


?>

 

But if I repeat the line: echo str_replace( "index.php", "", $file ) . " <br />"; and just replace it with a different file name It doesn't work because it repeats the other files names.

Why didn't you use the rename() function?

 

I don't want to rename the file, just rename it on that list.

I presume you mean you don't want the index.php file displayed in the generated list. You should do this instead:

<?php

$handle = opendir(".");

$ignore_files = array('.', '..', 'index.php');

while (($file = readdir($handle))!==false)
{
    // do not display files listed in the ignore_files array
    if(!in_array($file, $ignore_files))
    {
        echo $file . '<br />';
    }
}

closedir($handle);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.