exploding a file name to separate the name and extension won't work in PHP?

I'm building a class that returns an string that will include files in a folder automatically, kinda like a loader for an HTML file.

Here's the method that will be called:

function build_external_file_include($_dir){
    $_files = scandir($_dir);
    $_stack_of_file_includes = "";//will store the includes for the specified file.
foreach ($_files as $ext){
    //split the file name into two;
    $fileName = explode('.',$ext, 1);//this is where the issue is.

    switch ($fileName[1]){
        case "js":
            //if file is javascript
             $_stack_of_file_includes =  $_stack_of_file_includes."<script type='text/javascript' src='".$dir.'/'.   $ext ."'></script>";

            break;
        case "css";//if file is css
             $_stack_of_file_includes =  $_stack_of_file_includes."<link rel=\"stylesheet\" type=\"text/css\" href=\"".$dir.'/'. $ext."\" />";
            break;
        default://if file type is unkown
             $_stack_of_file_includes =  $_stack_of_file_includes."<!-- File: ".  $ext." was not included-->";
    }


}
return $_stack_of_file_includes;
}

So, this runs without any errors. But, it doesn't do what it's supposed to do... or at least what I intend it to do. Technically speaking here,

$fileName[1] should be the extension js

$fileName[0] should be name of the file main

but

$fileName[0] is main.js.

does explode not recognize .?

Thank you in advance.

Answers


You're forcing your resulting array to have 1 element, which causes it to have the entire filename.

explode( '.', $ext, 1 )

should instead be

explode( '.', $ext );

Proof: http://codepad.org/01DLpo6H


You've limited the explode to producing 1 array entry, so it can never do anything:

print_r(explode('.', 'a.b', 1));
Array
(
    [0] => a.b
)

The limit should be at least 2. or, better yet, you should be using the pathinfo() function, which properly handles filename components for you.


Need Your Help

Implied bit in IEEE floating point format

bit ieee-754

Why is there an implied (or hidden) bit in IEEE floating point format? What is the purpose of it? It is mentioned in passing on Wikipedia.

BackupBuddy Plugin Error

database wordpress plugins backup

When backing up the whole site. This is the Error

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.