Archive

Posts Tagged ‘file’

Text file encoding

March 23, 2011 1 comment

Detect the encoding of a text file:

$ file all.txt 
all.txt: ISO-8859 text

Get more verbosity:

$ file --mime all.txt 
all.txt: text/plain; charset=iso-8859-1

Change the encoding of a text file:

iconv --from-code=UTF-8 --to-code=ISO-8859-2 file.txt >tmp.txt

This latter tip is from here.

Update (20110918)
You can also use “chardet” for detecting charcter encoding. Usage:

$ chardet test.txt 
test.txt: utf-8 (confidence: 0.99)

Get file extension [PHP]

December 13, 2010 Leave a comment

Problem

You have a filename and you want to get its file extension, i.e. you want to extract the substring after the last dot.

Solution

/**
 * Return the file extension, i.e. take a string and return the
 * substring after the last dot.
 * If there is no extension, return an empty string.
 *
 * @return File extension (after the last dot) or empty string (if there is no extension).
 */
static public function get_extension($str)
{
	$ext = substr(strrchr($str, '.'), 1);
	return $ext;
}

Credits

I found this solution here. I chose this one because it’s simple enough and it returns an empty string if there is no file extension.

Categories: php Tags: ,
Follow

Get every new post delivered to your Inbox.

Join 43 other followers