1

I have a function that retrieves file names in some directory. The directory has multiple PDF files.

I want to assign a key to each of the PDF files I gathered, where the key is the number that is located between - and .pdf in the filename.

For instance I have 3 PDF files:

abc-1.pdf
abc-2.pdf
abc-3.pdf

What I want to have:

1 => abc-1.pdf,
2 => abc-2.pdf,
3 => abc-3.pdf

My function is currently this:

function getPDFs($PDF_DIR_NAME, $PDF_TOKEN) {
    if ($PDF_TOKEN == null || $PDF_TOKEN == '') {
        return null;
    } else {
        $getPDFVersions = $PDF_TOKEN.
        "*";
        $dirs = glob($PDF_DIR_NAME.
            '/'.$getPDFVersions);
        $files = array();
        foreach($dirs as $d) {
            if (filesize($d) > 200) {
                $files[] = basename($d);
            }
        }
        return $files;
    }
}

How can I assign keys for each PDF document?

5
  • $files[$key] = ... Commented Apr 13, 2017 at 9:00
  • 1
    Rather than assign, I think you mean you want to parse the number from the filename. Find the position of '-' and '.pdf' in the string, and use substr to get the part in between. Commented Apr 13, 2017 at 9:00
  • I want to assign the number between '_' and '.pdf' as key of each element in the array :) @SloanThrasher Commented Apr 13, 2017 at 9:11
  • Well, duh! To assign it, you have to know what it is. So you get the location of the '-', add 1, find the position of '.pdf', and subtract the 2nd from the first to get the length of the number, then use substr to get just the number part (ie the part of the string between "-" and ".pdf" Commented Apr 13, 2017 at 9:14
  • Lunatic, in your question, you state _, as in your comment. Your examples show a - though. Not too important, but you might want to be consistent. Also, I wrote up a bit of a guide for you but left the actual implementation to you. Commented Apr 13, 2017 at 9:15

2 Answers 2

2

Ingredients:

  1. strrchr(): Find the last occurrence of a character in a string
  2. substr(): Return part of a string
  3. (int) - Cast to integer (if required)

Recipe:

  1. Find the position of the (last) dash (-), as it comes before the ID.
  2. Find the position of the (last) dot (.), as it comes after the ID (see below)
  3. Strip out the ID with substr() and the information gathered in step 1 (and 2)
  4. Optionally, use the IDs to populate an associative array, like so:

    $myPDFs = array();
    $myPDFs[$id] = $filename;
    

Or whatever it is you need.

PS:

Instead of finding the position of the last dot, you could also simply assume that it is always 4 characters before the end of the filename. Seems a safe bet. Hint: substr() can take a negative number as its start parameter.

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

3 Comments

Hi, thanks for the great explanation. I will be trying to fetch the numbers of each file and assign them as the key of each element
Yep, give it a whirl. Make sure to report back if it worked out.
Great! Glad you were able to solve this with these pointers. Cheers. :)
0

Thanks to @domdom, his recipe worked perfectly :)

I added the code below to my foreach;

$versions= substr(strrchr(basename($d), "-"), 1);
$versions= preg_replace('/\\.[^.\\s]{3,4}$/', '', $versions);

then

$files[$qualities] = basename($d);

1 Comment

One thing though: will there ever be more than 9 PDF files? If so, you would need to get 2 characters to parse the number from the file name. Hence step 2 in my answer. Of course, if you know that there will never be more than 9 files, you're good.

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.