Skip to content
Advertisement

Removing a .jpg extension from a string in php

I have the following code for a gallery. When the thumbnail is clicked, I would like to open the big image in another window (which it does) and then be able to navigate to the next image in the new window, but in order to do this, I need to strip the .jpg off the filenames so that I can just ‘+1’ to the filename, as they are sequentially numbered.

For example, files are numbered 001.jpg, 002.jpg, 003.jpg etc.

I currently have:

    echo '<p>basename=' . basename($i) . '</p>';

Which give the name of the file with .jpg extension, for example basename=001.jpg.

Then I have :

    $image = basename($i); // to give the variable $image
    $img = str_replace('.jpg',$image, ''); //to take off the .jpg extension

but the output I expect, 001, doesn’t echo. It just has nothing… img=

    echo '<p>img=' . $img . '</p>'; 

What am I doing wrong? Can anyone point me in the right direction?

Many thanks, Kirsty

Advertisement

Answer

Try this

$YourPicture = 'cat.jpg';
$without_extension = pathinfo($YourPicture, PATHINFO_FILENAME);

result will be

cat
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement