extract file extension, php extract file, php expode, php end, php strrchr, php substr, php pathinfo

How to get file or link extension from filename in PHP

As we all know that every file or link has a suffix that determines the type of the file or link. The suffix is called file extension or link extension.

Even if in other files or links it may not be visible or displayed in the file or in the link, it’s just that it is hidden but it’s there.

Example of a file with an extension

In your computer directories, you have files such as example1.pdf or whatsapp_image.jpg. The suffix in each of the files are known as the extensions, that is,

In example1.pdf, the suffix is .pdf and that is the extension of the defined file.

In whatsapp_image.jpg, the suffix is .jpg which is the extension of the defined document or file.

The extension in a file or document defines the type of file or document it is, that is, in the examples, we have highlighted above, example1 is a pdf document as it has a .pdf extension while whatsapp_image.jpg is an image of type jpg due to the extension it has.

Example of a link with an extension

The links in a browser like this one https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php have an extension  .php

The extension in a link defines the programming language used to develop the file that can be accessed using the link provided

The extension in a file, document, or link determines how it will be opened since every document type has a different software associated with it which allows the content in it to be visible to a user.

For example, a pdf document has applications like adobe pdf reader while .png/jpg is opened with photo viewer-related apps. Links are opened with browsers like google chrome, firebox, and related software.

If you try to open a file with software not associated with it or not incompatible, the contents will not be displayed to you.

How to extract or get file or link extension in PHP

As we discussed in a previous article, we noted the different types of functions that perform different types of services depending on what the user wants. Refer the different types of functions in PHP

To extract file extension or link extension from a file name using PHP, we use provided inbuilt functions that are already provided by PHP language.

In some cases, only one function is used to get the file or link extension from the name while in other cases a combination of more than one function is used to achieve the result depending on the type of document or link provided.

How to extract extension from a link or document

Suppose we have a link like this https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php or a document like abc.pdf

Please note that the methods discussed below are used for either link, documents, or file to get the extension

Method 1

To extract and get the extension from the above link, we use the steps provided.

  • Use explode() function which separates the link in two different parts at the point where there is (.) as shown

<?php

$link_name = 'https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php';

 $array= explode('.',$link_name);

?>

The above query returns an array with two parts as shown below

["https:\\/\\/www","solutionspacenet","com\\/post\\/how-to-embed-maps-in-a-website-using-iframe","php"]

To get the extension from the array, we use the end() function which returns the last item in the array as shown

$extension = end($array);

The full code to extract the extension from a link is as shown

<?php

$link_name = 'https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php';

 $array= explode('.',$link_name);

$extension = end($array);

echo $extension;

?>

The result of the above code is php. Note it is not .php but php without the (.)

Method 2

Another way to extract the extension from a link is using the combination of strrchr() and substr() inbuilt PHP functions.

The strrchr() function gets the last occurrence from a string starting from a defined character, in this case we want to get the last occurrence from the (.) therefore we define it as below

<?php

$link_name = 'https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php';

echo $last_occurence=strrchr($link_name, '.');

The substr() function is used to return a part of the string starting from a defined section, in this case since we have .php, we will return the part starting from position 1 as shown

$extension = substr($last_occurence, 1);

   echo  $extension;

The full code to extract the extension using the strrchr() and substr() functions is a shown

<?php

$link_name = 'https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php';

 $last_occurence=strrchr($link_name, '.');

 $extension = substr($last_occurence, 1);

   echo  $extension;

?>

Method 3

We use the pathinfo() function and then use the PATHINFO_EXTENSION to ensure that the file or link extension will be returned.

The pathinfo() function alone returns an array while we add the PATHINFO_EXTENSION, the result displayed will be the file extension only

An example using the pathinfo() function is as shown

<?php

$link_name = 'https://www.solutionspacenet.com/post/how-to-embed-maps-in-a-website-using-iframe.php';

 $extension = pathinfo($link_name, PATHINFO_EXTENSION);

    echo $extension;

?>

That’s it for this discussion, you can use any of the above methods depending on which suits you well to get the extension from a file name using PHP