I don't seem to know the right search term so I can't find the answer to this one. I have a simple php script that retrieves a filename in the form of name.ext. When I echo that to the screen I wish to delete the .ext. How can you parse a variable to remove part of the string? In all cases it would of course be the part after the period but might vary from 3-4 characters. If parse is an incorrect term for this, correct me.
In PHP there's lots of functions that let you work with strings. For this particular example (assuming your file names have only one dot in them, separating filename and extension) you could just use: // let's assume $filename_with_extension = 'somefile.ext' list($filename, $extension) = explode('.', $filename_with_extension); echo $filename // outputs 'somefile' echo $extension // outputs 'ext' PHP: What explode() does is splitting a string into an array of strings. The splitting happens in this example everywhere where a dot occurs.
You have good answers already but here is one more way to do it, using basic string functions that you will want to learn about anyway: $str = "filename.jpg"; $base = substr ($str, 0, strpos ($str, ".")); echo "Filename: \"$str\"<br/>"; echo "Basename: \"$base\"<br/>"; PHP:
If you have PHP 5.2.0 +, you can use pathinfo() $file = 'file.ext'; $parts = pathinfo($file); echo $parts['filename']; // file PHP:
This will work with any PHP version: <? function extract_name($filename) { $pos=0; $pos2 = 0; $pos = strpos($filename,'.',0); // find the first occurance of . if ($pos===FALSE) { return $filename; // filename has no . so no extension, return $filename } while ($pos!==FALSE) // find the last occurance of . in the $filename {$pos2 = $pos; $pos = strpos($filename,'.',$pos+1); } if ($pos2==0) return $filename; // should never happen but just in case else return substr($filename,0,$pos2); // return everything before the last . } echo extract_name("filename.ext"); ?> PHP: It works even with any file extension length by looking for the last dot in the string and returning what's on the left of that dot. If no extension found, the function returns the complete string.
I used the method posted by redokai above because it looked like the easiest to implement into the existing script. It works but now I am getting duplicate entries for some but not all of the files in the directory. Except for the path statement at the top of the script, here is the entire script. Can anyone see what I did wrong? //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); echo "Contents<br/>"; //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") {$name=$file; list($name, $extension) = explode('.', $name); } echo "<a href='uploads/$file'>$name</a><br/>"; } //closing the directory closedir($dir_handle); PHP: Many thanks to all who have replied.
According to php.net a readdir loop should look like this: //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); echo "Contents<br/>"; //running the while loop while (false !== ($file = readdir($dir_handle))) { if($file!="." && $file!="..") { $name=$file; list($name, $extension) = explode('.', $name); echo "<a href='uploads/$file'>$name</a><br/>"; } } //closing the directory closedir($dir_handle); PHP: (and I also cleaned up what looked like a logic error). Not sure if that will fix duplicate entries, though. Maybe you have some files that are identical except for the extension?
coderbari, you should really reconsider using the function I've written. The method with explode works, no problem at all, but not in all cases. For example, If you have a file called archive.tar.gz do you really want the name to be archive or archive.tar ? Same thing for "Finance Document 100. Reviewed copy.doc" .... Returning the text on the left of the first dot is not really a good idea.
strrpos will deal with that one. For double extension files, that's compressed one. Don't know if they need that. the simple way,using regexp expresions: $name = preg_replace('/\.[^.]*$/', '', $FileName); Code (markup):
yes mariush i agree with you.i never told your function is wrong it should be done using strpos() to get the last occurance of '.' and then removing the last portion of the filename
Thanks a million! That did the trick. Not sure what you are calling the logic error but that must have been the problem with the duplicates. I notice you got the variable reset ($file=$name) on a line of it's own. Again many thanks to all. edit: the duplicate entries were with the file output. The first and fourth of 5 files were displayed in the output twice. As I said above the problem is solved.