PHP strings - substr() function

substr() is an usefull function to easily manipulate strings and use them, it returns a custom segment of a whole string.

substr, from position $n to end:
You can extract a small segment from a string from a position $n to the end:
<?php
$part = substr("I'm in Europe now",
7 ); // $part = "Europe now"
?>
Note: position counting start from position 0 !

substr, from position $n1 to $n2 :
You can choose, in substr() parameters, how many characters to extract after the position $n:
<?php
$part = substr("I'm in Europe now",
7 , 6 ); // $part = "Europe"
$part = substr("I'm in Europe now", 7 , 3 ); // $part = "Eur"
?>
Extract $n characters from the end of the string:
You can output the $n last character from a string:
<?php
$part = substr("I'm in Europe now",
-3 ); // $part = "now"
?>
Negatif parameters...

You may choose the start position with a negatif number, from the end of the string ( like we see in the previous code):
<?php
$part = substr("I'm in Europe now",
-3 ); // $part = "now"
$part = substr("I'm in Europe now", -3 , 2 ); // $part = "no"
?>
If te lenth parameter is negatif, it will count the end of the extracted string from the end...
<?php
$part = substr("I'm in Europe now", 7 , -4 ); // $part = "Europe"
?>
7 is the position whare the extraction should start, and -4 is the ending position counting from the end, so the fourth position from the end is the "e" character...

PHP some filesystem functions

PHP have many functions to acces and manipulate files, the most important filesystem functions are posted here...

basename() : Returns the filename component from a path.
<?php
$path = "/directory1/index.html";
echo basename($path);
// display: index.html
echo basename($path ,".html" ); // display: index
?>

file_exists() : Checks if a file or directory exists ot not.
<?php
echo file_exists("test.txt"); // returns True if exists, otherwise returns False
?>

rename() : Renames a file or directory.
<?php
rename("dir1","dir2"); // You can rename a file or a folder
?>

copy() : Copies a file.
<?php
copy("sourcefile.txt","destinationfile.txt");
?>

unlink() : Deletes a file.
<?php
unlink("file.txt"); // returns True if deleted successfully, False if not deleted.
?>



filesize() : Returns the size of the file.
<?php
echo filesize("file.txt"); // returns the size of file.txt in bytes, or False in failure.
?>

filetype() : Returns the file type.
<?php
echo filetype("file.txt"); // returns type: file.
//Possible types are: file, dir, link, block, char, fifo, unknown.
?>

is_readable() : Checks whether a file is readable, returns true if yes.
is_writable() : Checks whether a file is writeable, returns true if yes.

fileperms() : Returns the permissions of a file.
filemtime() : Returns the last modification time of a file, Display a number in second, to convert it to humain redable date format, use:
<?php
echo date("F d Y H:i:s", filemtime("test.txt")); // returns date format: September 03 2008 18:27:35
?>


If you have any question, or need more details or functions, post a comment here.

Get Filename without extension

Hello,

It's sometimes usefull to pick the name of the file without its extension, and this is very easy with php !

PHP Code:
<?php
$arr = explode(".", $allname);
$filename = $arr[0];

?>
But this script will not work the file name is dotted, in that case use this:

PHP Code:
<?php
$filename = preg_replace( '/\.[a-z0-9]+$/i' , '' , 'dotted.file.Name' );

?>
Or:

<?php
$FileNameTokens = explode('.', $allname);
$fileName = implode(".", array_slice($FileNameTokens, 0, count($FileNameTokens) - 1));

?>

If this is a few hard to understand, this script is easier:

PHP Code:
<?php
function getFilenameWithoutExt($filename){
$pos = strripos($filename, '.');
if($pos === false){
return $filename;
}else{
return substr($filename, 0, $pos);
}
}

?>

I hope this will help you.