2

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!

2
  • Not a solution but a hint. Your expression can be simplified to ~\D*\d{3}-\d{4}\D*~ - there is no need to put \D in a character class and [0-9] equals to \d in PCRE. Commented Jul 7, 2016 at 16:37
  • Try $regex = "/(?<!\d)[0-9]{3}-[0-9]{4}(?!\d)/"; Commented Jul 11, 2016 at 17:59

1 Answer 1

3

You're feeding the filenames to preg_match_all one at a time, and each time $result gets overwritten. The thing is to push $results[0] onto an array each time, and then dump that array.

$final_results = [];
while ($file = readdir($handler)) {
    if ($file != "." && $file != "..") {
        $regex = "/[\D]*[0-9]{3}-[0-9]{4}[\D]*/";
        if (preg_match_all($regex, $file, $results) > 0)
            $final_results[] = $results[0] ;
    }
}
print_r ($final_results);

Updated per comment: this version creates a key=>value array where the phone number is the key and the value is all the filenames that contain that number.

$final_results = [];
while ($file = readdir($handler)) {
    if ($file != "." && $file != "..") {
        $regex = "/[\D]*[0-9]{3}-[0-9]{4}[\D]*/";
        if (preg_match_all($regex, $file, $results) > 0) {
            if empty ($final_results[$results[0]]) {
                $final_results[$results[0]] = $file ;
            } else {
                $final_results[results[0]] .= " ".$file ;
            }
        }
    }
}
print_r ($final_results);

Once you've got this array, it's trivially easy to flatten it to a one-dimensional array like you want.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! Any thoughts how to have results with same phone number in same array line?
Updated answer. I didn't test the code but the outline is right. If this answer works for you, be sure to check as accepted, thanks!
all look ok to me, but i get HTTP ERROR 500
Hey Juan, can you take a look why there is HTTP ERROR 500?
@Vlad Sorry, no I can't. Maybe I'm using empty() on a non-object? Perhaps ! isset() would work better. But 500 error is impossibly vague. If you want to know what the real problem is, run your code in a console and not on a remote server. Then you can print debug information and see a stack trace when your code crashes. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.