0

I have the following php:

               <?php 
            $image_directory = JPATH_SITE . '/images/site/';
            $images = JFolder::files($image_directory);
            foreach($images as $image) {
              echo '<li><img src="/images/site/'.$image.'">
                    <p class="slidecaption">'.$image.'</p></li>'; 

            }
          ?>

So $images ends up being 1980.jpg (for example). How can I perform an operation on this string and strip the ".jpg" and output for the slidecaption paragraph?

Many thanks for your time.

j00m
  • 491
  • 5
  • 20
  • 1
    When PHP provides a [built-in function](http://php.net/manual/en/function.pathinfo.php ) specifically for this purpose, use it – Mark Baker Feb 07 '15 at 23:23

1 Answers1

0

If you have every time the same extension then you can use str_replace.

str_replace('.jpg','',$image);

If you have some other more extensions you can do it with a regex something like this:

$imageName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image);
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • What if it's in the file name more than once? Grabbing the last . In the string and everything after it would probably be better in the long run – scrowler Feb 07 '15 at 22:59
  • When there is a .jpg twice in your string then it will remove both of them and this happens only if there is something wrong and you have to times the file extension. But then your name is correct. – René Höhle Feb 07 '15 at 23:02
  • 1
    That is really wrong... basename don't remove the file extension ;) `echo basename("test.jpg");` and when you define a suffix as `.jpg` then its the same like the str_replace. http://php.net/manual/en/function.basename.php – René Höhle Feb 07 '15 at 23:07