shamuraq Posted March 18 Share Posted March 18 Hi guysm I have a folder containing these files: alg001.php alg002.php alg003.php alg004.php alg005.php algComp.php ranFrac.php ranalg.php randec.php randint.php I would only like to store the names of files alg001.php, alg002.php, alg003.php, alg004.php, alg005.php into an array. How do i achieve this with preg match? Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/ Share on other sites More sharing options...
Barand Posted March 18 Share Posted March 18 Easier with str_starts_with() $files = [ 'alg001.php', 'alg002.php', 'alg003.php', 'alg004.php', 'alg005.php', 'algComp.php', 'ranFrac.php', 'ranalg.php', 'randec.php', 'randint.php' ]; $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00')); echo '<pre>' . print_r($required, 1) . '</pre>'; Gives Array ( [0] => alg001.php [1] => alg002.php [2] => alg003.php [3] => alg004.php [4] => alg005.php ) Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/#findComment-1618124 Share on other sites More sharing options...
shamuraq Posted March 18 Author Share Posted March 18 32 minutes ago, Barand said: Easier with str_starts_with() $files = [ 'alg001.php', 'alg002.php', 'alg003.php', 'alg004.php', 'alg005.php', 'algComp.php', 'ranFrac.php', 'ranalg.php', 'randec.php', 'randint.php' ]; $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00')); echo '<pre>' . print_r($required, 1) . '</pre>'; Gives Array ( [0] => alg001.php [1] => alg002.php [2] => alg003.php [3] => alg004.php [4] => alg005.php ) I tried as suggested: $arrFiles = array(); $dirPath = "./"; $files = scandir($dirPath); $required = array_filter($files, fn($v)=>str_starts_with($v, 'alg00')); echo '<pre>' . print_r($required, 1) . '</pre>'; The error: Fatal error: Uncaught Error: Call to undefined function str_starts_with() ...php:29 Stack trace: #0 [internal function]: {closure}('.') #1 /...php(29): array_filter(Array, Object(Closure)) #2 {main} thrown in ...php on line 29 Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/#findComment-1618125 Share on other sites More sharing options...
Solution Barand Posted March 18 Solution Share Posted March 18 You must be using an outdated version of php. Str_starts_with() requires version 8. Use this instead $required = array_filter($files, fn($v)=>substr($v, 0, 5) == 'alg00'); Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/#findComment-1618127 Share on other sites More sharing options...
shamuraq Posted March 18 Author Share Posted March 18 (edited) 14 minutes ago, Barand said: You must be using an outdated version of php. Str_starts_with() requires version 8. Use this instead $required = array_filter($files, fn($v)=>substr($v, 0, 5) == 'alg00'); What does the values in 0,5 in substr($v, 0, 5) refers to? Edited March 18 by shamuraq Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/#findComment-1618130 Share on other sites More sharing options...
Barand Posted March 18 Share Posted March 18 Yes - the code I told you to use instead. You'll benefit more if you read the replies. Quote Link to comment https://forums.phpfreaks.com/topic/319155-using-preg-match-to-find-specific-cluster-of-files/#findComment-1618131 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.