cheechm Posted April 18, 2008 Share Posted April 18, 2008 Hi, Got this at the moment: if (!$action) { // directory path can be either absolute or relative $dirPath = '/home/niick/public_html/qw/forum/language/en/mods/'; // open the specified directory and check if it's opened successfully if ($handle = opendir($dirPath)) { // keep reading the directory entries 'til the end while (false !== ($file = readdir($handle))) { // just skip the reference to current and parent directory if ($file != "." && $file != "..") { if (is_dir("$dirPath/$file")) { $template->assign_vars(array('S_DIR' => true)); $template->assign_block_vars('language', array('DIR' => $file)); // found a directory, do something with it? } else { // found an ordinary file $template->assign_block_vars('language', array('FILE' => $file)); } } } // Close what we opened closedir($handle); } } I want to remove the extension (ie .php / .html) when the files are listed. Is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/ Share on other sites More sharing options...
947740 Posted April 18, 2008 Share Posted April 18, 2008 Use a regex to replace everything after a "." with "". With that, there cannot be any other periods in the string unless you modify the regex a little bit more. Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520640 Share on other sites More sharing options...
Zhadus Posted April 18, 2008 Share Posted April 18, 2008 If you have file names that contain "." also, you can explode the strings with "." and count the number of values in the array. Remove the last index and implode the remaining indexes with ".". Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520648 Share on other sites More sharing options...
cheechm Posted April 18, 2008 Author Share Posted April 18, 2008 If you have file names that contain "." also, you can explode the strings with "." and count the number of values in the array. Remove the last index and implode the remaining indexes with ".". Could you give me some code for that? Just like a starting code or something. Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520654 Share on other sites More sharing options...
Zhadus Posted April 18, 2008 Share Posted April 18, 2008 I apologize, implode will not work, but here's some code and a little loop to do what I had meant by using implode(). <?php $file = 'index.php'; $file2 = 'index.file.php'; $filearray = explode('.', $file); $filearray2 = explode('.', $file2); $filearray[(sizeof($filearray)-1)] = ""; $filearray2[(sizeof($filearray2)-1)] = ""; $file = ""; for ($x = 0; $x < (sizeof($filearray)-1); $x++) { $file .= $filearray[$x]; if ($x !== (sizeof($filearray)-2)) $file .= '.'; } $file2 = ""; for ($x = 0; $x < (sizeof($filearray2)-1); $x++) { $file2 .= $filearray2[$x]; if ($x !== (sizeof($filearray2)-2)) $file2 .= '.'; } echo $file . "\n"; // Will output "index" echo $file2; // Will output "index.file" ?> Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520662 Share on other sites More sharing options...
Daniel0 Posted April 18, 2008 Share Posted April 18, 2008 Or: <?php $filename = 'file.test.php'; echo substr($filename, 0, strrpos($filename, '.')); // output: file.test // OR echo pathinfo($filename, PATHINFO_FILENAME); // output: file.test ?> Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520664 Share on other sites More sharing options...
Zhadus Posted April 18, 2008 Share Posted April 18, 2008 I just got completely owned by that code. Daniel0's code is definitely a little easier, simpler, and more efficient Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520669 Share on other sites More sharing options...
discomatt Posted April 18, 2008 Share Posted April 18, 2008 <?php function remove_ext ( $filename ) { $arr = explode('.', $filename); if ( count($arr) === 1 ) return $filename; else return substr( $filename, 0, (strlen( end($arr) ) + 1) * -1 ); } $file = 'this.is.a.test.jpg'; echo remove_ext($file); ?> Hope that helps [edit] using pathinfo() is probably best here [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520671 Share on other sites More sharing options...
cheechm Posted April 18, 2008 Author Share Posted April 18, 2008 Big thanks as usual guys. Quote Link to comment https://forums.phpfreaks.com/topic/101761-solved-directories/#findComment-520688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.