find the extension of a json file in php

To find the extension of a json file in PHP, you can use the pathinfo() function. It returns an associative array containing information about the file path including the extension.

main.php
<?php
$file = 'example.json';
$info = pathinfo($file);
$ext = $info['extension'];
echo $ext; // outputs "json"
?>
114 chars
7 lines

In the above code, we first define the file path and then pass it to the pathinfo() function. The function returns an array containing the extension of the file amongst other things. Finally, we extract the extension from the array using the ['extension'] value and output it to the screen.

related categories

gistlibby LogSnag