What are the best practices for extracting and manipulating file names in PHP to achieve the desired result?

When working with file names in PHP, it is important to use built-in functions such as pathinfo() to extract information like the file extension or file name. Manipulating file names can be done using functions like basename() to get the base name of a file or str_replace() to replace certain parts of the file name. By following these best practices, you can easily extract and manipulate file names in PHP to achieve the desired result.

// Example code to extract and manipulate file names in PHP

// Get the file name without the extension
$filename = 'example.txt';
$filenameWithoutExtension = pathinfo($filename, PATHINFO_FILENAME);

// Replace a part of the file name
$newFilename = str_replace('example', 'new', $filename);

echo $filenameWithoutExtension; // Output: example
echo $newFilename; // Output: new.txt