How to exclude image extension from string place in php?

Member

by samara , in category: PHP General , 3 months ago

How to exclude image extension from string place in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 3 months ago

@samara 

You can use the pathinfo function in PHP to get the extension of a file, and then use str_replace function to remove the extension from the string. Here is an example code to exclude image extension from a string:

1
2
3
4
5
$filename = "example.jpg";
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$stringWithoutExtension = str_replace(".".$extension, "", $filename);

echo $stringWithoutExtension; // Output: example


In this code, we first get the extension of the filename using pathinfo function. Then, we use str_replace function to remove the extension from the original filename. The resulting string will be the filename without the extension.