Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with array "cleaning"

Status
Not open for further replies.

ronnie98

Technical User
Aug 5, 2004
36
IT
Hi all,
i have this array
Code:
Array ( [0] => . [1] => .. [2] => serverstats.php [3] => phpstats.php [4] => phpstats.php~ [5] => serverstats.php~ )
It retrieves the values by scanning a directory where there are only two files
Code:
serverstats.php and phpstats.php
For some strange(for me) reason it also loads
Code:
serverstats.php~ and phpstats.php~
into the array.
My goal is to clean the array above from "~" and ".php"
and put only the filenames into variables like these:
Code:
$first_file = "serverstats";
$second_file = "phpstats";
Thanks in advance for helping.
 
Have you considered using glob() to just retrieve the files by specific pattern?
That way you could just load all .php files and no celaning would be necessary.
 
I did something like this but for includes not for storing into an array.

Here is the code that can be modified for it
Code:
	if($handle = opendir("{$CONFIG['root']}/sources/mods")){ // This checks that the directory is opened if not it doesn't fire
		while(false !== ($filename = readdir($handle))){ // This loops through all files in the directory
			if(preg_match('/m_([^\.php]+)\.php/',$filename)){ // this looks for files that start with m_ and end with .php. 
				include("mods/{$filename}");
			}
		}
		closedir($handle);
	}

It uses a Regular Expression to ensure that only the files I want are accessible.

JRSofty
 
Thank you JRSofty,
your code helped me to find the solution.
ATB,ronnie
 
Again, why write your own function if PHP has a native function that does exactly the same?
Code:
foreach (glob("*.php") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
}
How could it be more simple?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top