such as the suffix of picture files, jpg or GIF, etc.
There are two ways
One, if $img for picture file name
$img =12345.gif;
$img _ext = substr ($img, Strrpos ($img, '. '));
Second, use the PHP array, first read the filename string into the array
$ext 2=explode (".", $img);
$count =count ($ext 2);
$count 2= $count-1;
$file _name = $urlstr. $ext 2[$count 2];//get the suffix and redefine the new file name $file_name
Also attached are the methods summed up by other small partners:
Method 1th:
function get_extension ($file)
{substr (
strrchr ($file, '. '), 1);
}
2nd method:
function Get_extension ($file)
{return
substr ($file, Strrpos ($file, '. ') +1);
}
3rd method:
function Get_extension ($file) {return end (
explode ('. ', $file));
}
The 4th method:
function Get_extension ($file)
{
$info = PathInfo ($file);
return $info [' extension '];
}
The 5th method:
function Get_extension ($file)
{return
pathinfo ($file, pathinfo_extension);
}
The above several ways coarse look, seems to be all right, especially 1, 2 kinds of methods, before I do not know PathInfo has the second parameter has been used. But think about it, the first four methods have all kinds of problems. To get the file extensions exactly right, you must be able to handle the following three special cases.
- No file name extension
- The path contains characters. such as/home/test.d/test.txt
- The path contains characters., but the file does not have an extension. such as/home/test.d/test
Obviously: 1, 2 can not deal with the third situation, 3 can not correctly deal with the 13th case. 4 can be handled correctly, but a warning is issued when no extension exists. Only the 5th method is the right approach. By the way, look at the PathInfo method. The official website introduces as follows:
$file _path = pathinfo ('/www/htdocs/your_image.jpg ');
echo "$file _path [' dirname ']\n";
echo "$file _path [' basename ']\n";
echo "$file _path [' extension ']\n";
echo "$file _path [' filename ']\n]; Only in PHP 5.2+
It returns an array containing up to four elements, but does not always have four, for example, without an extension, there will be no extension element, so the 4th method will find the warning. But Phpinfo also supports the second parameter. You can pass a constant that specifies the data to return a part of:
Pathinfo_dirname-Directory
pathinfo_basename-file name (including extension)
pathinfo_extension-Extension
pathinfo_filename- filename (without extension, php>5.2)
The values for these four constants are 1, 2, 4, 8, and at first I thought you could specify multiple by or operation:
PathInfo ($file, Pathinfo_extension | Pathinfo_filename); The
later found that this would not work, which would only return the smallest of several constant or operational constants. That is, a constant with a minimum of 1 digits in four flags.