Gentacti
Gentacti

Reputation: 1

Numbering automatically from file name

I have a list of files in a directory and we want them to be numbered like this:

  1. file1
  2. file2
  3. file3

Each file is already numbered, we just need to "extract" the number from the file name. How can I do it in PHP?

Thanks a lot

Upvotes: 0

Views: 118

Answers (1)

Emma
Emma

Reputation: 27733

I'm sure there are many ways to solve your problem. This might work:

$path = '/Library/Of/MyPC/path/to/files/';
$numbers = filter_var_array(array_values(glob($path . "*")), FILTER_SANITIZE_NUMBER_INT);
var_dump($numbers);

Output

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

If it did not work, you may look into these or similar links 1, 2, 3, 4, 5, which might help you.

Upvotes: 1

Related Questions