I'm trying to add filenames with phone numbers in array. My test files are:
dsfdsf543-6786sdfsdfd.jpg
543-6786sdfsdfd.jpg
435-3454
dsfdsf543-6786.jpg
123-4567
543-6786.jpg
345-3454
My goal is different phone numbers will have separate line of elements in array. Each element in array line will have same phone number. For example:
543-6786 dsfdsf543-6786sdfsdfd.jpg 543-6786sdfsdfd.jpg dsfdsf543-6786.jpg 543-6786.jpg
435-3454
123-4567
345-3454
My code:
$directory = $_SERVER['DOCUMENT_ROOT'];
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
$regex = "/[\D]*[0-9]{3}-[0-9]{4}[\D]*/";
preg_match_all($regex, $file, $results);
}
}
print_r ($results);
Result is
Array ( [0] => Array ( [0] => 345-3454 ) )
why only one filename in the array? Where is my mistake? Thank you in advance!
~\D*\d{3}-\d{4}\D*~- there is no need to put\Din a character class and[0-9]equals to\dinPCRE.$regex = "/(?<!\d)[0-9]{3}-[0-9]{4}(?!\d)/";