I do not understand if you want to get the files, or if they are already listed and you want to extract the number forward.
If you want to extract the number you can use explode
so (this way the explode will only divide by the first underline):
<?php
$filename = '20160111_ALGUMA_COISA.txt';
list($id, $name) = explode('_', $filename, 2);
echo 'Id: ', $id, PHP_EOL;
echo 'Nome: ', $name, PHP_EOL;
You can also use strtok
:
<?php
$filename = '20160111_ALGUMA_COISA.txt';
$id = strtok($filename, '_');
$name = strtok('');
echo 'Id: ', $id, PHP_EOL;
echo 'Nome: ', $name, PHP_EOL;
If you want to remove the extension you can use rtrim
, like this:
<?php
$filename = '20160111_ALGUMA_COISA.txt';
list($id, $name) = explode('_', $filename, 2);
$name = rtrim($name, '.txt');
echo 'Id: ', $id, PHP_EOL;
echo 'Nome: ', $name, PHP_EOL;
Or:
<?php
$filename = '20160111_ALGUMA_COISA.txt';
$id = strtok($filename, '_');
$name = strtok('');
$name = rtrim($name, '.txt');
echo 'Id: ', $id, PHP_EOL;
echo 'Nome: ', $name, PHP_EOL;
Now if what you want is to list the files that begin with numbers you can try to use glob
:
<?php
foreach (glob('[0-9]*[_]*.txt') as $filename) {
echo $filename, '<br>';
}
Documentation: