jacko310592 Posted November 18, 2009 Share Posted November 18, 2009 i have the following code which shows all the file names of files on my webiste... <?php $files = new RecursiveDirectoryIterator("./"); foreach(new RecursiveIteratorIterator($files) as $files) { $files = basename($files); echo ''.$files.'<br/>'; } ?> my problem is that i only want this code to find files with a '#' at the begining of the files name, can anyone think of a way this code can be altered to do this. thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/ Share on other sites More sharing options...
Ken2k7 Posted November 18, 2009 Share Posted November 18, 2009 Try this: <?php $files = new RecursiveDirectoryIterator("./"); foreach(new RecursiveIteratorIterator($files) as $files) { $file = basename($files); if (strpos($file, '#') == 0) echo ''.$file.'<br/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960251 Share on other sites More sharing options...
newbtophp Posted November 18, 2009 Share Posted November 18, 2009 <?php $files = new RecursiveDirectoryIterator("./"); foreach(new RecursiveIteratorIterator($files) as $files) { $file = basename($files); if(preg_match('/^[#]/', $file)) { echo ''.$file.'<br/>'; } } ?> Edited: Fixed regexp - thanks mikesta707 Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960255 Share on other sites More sharing options...
mikesta707 Posted November 18, 2009 Share Posted November 18, 2009 just so you know newb, your example will work for file names with the character # anywhere, rather than strictly at the top $files = array("#file1", "file2", "file3", "fil#", "#file5"); foreach($files as $files) { $files = basename($files); if(preg_match('/#/', $files)) { echo ''.$files.'<br/>'; } } output: #file1 fil# #file5 you probably meant foreach($files as $files) { $files = basename($files); if (preg_match('/^[#]/', $files)){ echo ''.$files.'<br/>'; } } the ^ part matches to the beginning of the string Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960259 Share on other sites More sharing options...
jacko310592 Posted November 18, 2009 Author Share Posted November 18, 2009 thanks for the reply Ken2k7, but that code continues to show all the files on the site- also thanks to you newbtophp for a reply, but i got an error message when i tried your code :/ but mikesta707, you code worked perfectly, thanks allot Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960264 Share on other sites More sharing options...
Ken2k7 Posted November 18, 2009 Share Posted November 18, 2009 Oh sorry, I should have used 3 equal signs. Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960270 Share on other sites More sharing options...
jacko310592 Posted November 18, 2009 Author Share Posted November 18, 2009 ahhh, now your code works, nice work (Y) Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960279 Share on other sites More sharing options...
Ken2k7 Posted November 18, 2009 Share Posted November 18, 2009 For clarification purposes, one of the values strpos returns is false if the string is not found. And with 2 equal signs, false and 0 are the same. But they are not the same with 3 equal signs, which also check for types. Quote Link to comment https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/#findComment-960289 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.