简体   繁体   中英

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

Try this

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

result will be

cat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM