Take the 2-minute tour ×

Right Now I Have:

$path2 = $file_list1; 
$dir_handle2 = @opendir($path2) or die("Unable to open $path2"); 
while ($file2 = readdir($dir_handle2)) { 
if($file2 == "." || $file2 == ".." || $file2 == "index.php" ) 
echo ''.$file2.'<br />'; 
echo '<br />';

When $file2 is returned, the last 4 characters in the string will always end in a number plus the file extension .txt, like this:


So my question is, how can I separate $file2 so it returns the string in two parts, $file_name and $call_number like this?:

echo 'File: '.$file_name.' Call: '.call_number.'<br />';


File: file_name_here Call: 1
File: some_other-file Call: 10

instead of this:





share|improve this question
Can the filename contain digits as well? If yes, what do you do if the original filename ends with a digit (thus, cannot be distinguished from the "call number")? –  Felix Kling Feb 9 '11 at 17:22
add comment

4 Answers

up vote 1 down vote accepted

I'm a big advocate of Regex but I decided to go slightly different here. Check it out:

$file = 'file_name_here19.txt';
$file_parts = pathinfo($file);
$name = $file_parts['filename'];
$call = '';
$char = substr($name, strlen($name) - 1);
while(ord($char) >= 48 && ord($char) <= 57) {
    $call = $char . $call;
    $name = substr($name, 0, strlen($name) - 1);
echo 'Name: ' . $name . ' Call: ' . $call;
share|improve this answer
This seems to be my solution, however if the file name ends in a number, it will be added to $call, as mentioned. Therefore, suppose each file name ends in "file_name_1__123.txt, this_file_name_2__456.txt" where the ending is always "2 underscores,call#,.txt" How do I modify your answer accordingly? –  mobilestimulus Feb 9 '11 at 19:53
Oh I see, what you'd want to do is just another substr() call after the while loop. Something like $name = substr($name, 0, strlen($name) -2); That's not tested so just make sure the -2 is the correct amount for chopping off the underscores. –  Aaron Hathaway Feb 9 '11 at 20:02
Bingo! Thank you much.... –  mobilestimulus Feb 9 '11 at 20:15
add comment
  1. Use pathinfo() function to cut off file extension.
  2. Use preg_match() function to separate name from number. 3.

    while (...) {
        $filename; // some_other-file10.txt
        $filename = pathinfo($filename, PATHINFO_FILENAME); // some_other-file10
        preg_match('/^(?<name>.*?)(?<number>\d+)$/', $filename, $match);
        $match['name'];   // some_other-file
        $match['number']; // 10
        echo "File: {$match['name']} Call: {$match['number']}\n";
share|improve this answer
basename only strips the suffix if you specify it. And what should .*+ be in your expression? Did you mean .*? ? –  Felix Kling Feb 9 '11 at 17:22
Ops... my mistake, fixed. –  Crozin Feb 9 '11 at 17:35
add comment

Try this, you need to use Regex to do this effectively

$filename = reset(explode(".", $file2))
preg_match("#(^[a-zA-Z\_\-]*)([\d]*)#", $filename, $matches);
$fullMatch = $matches[0];
$file = $matches[1];
$call = $matches[2];

echo "File: " . $file . " Call: " . $call;
share|improve this answer
add comment

Use regular expressions:

preg_match("/^(.+)(\d+)(\..+)$/", $file2, $matches);
$file_name = $matches[1];
$call_number = $matches[2];
share|improve this answer
add comment

Your Answer


