Substr from end of line php?

I have such an array, I will make it very easy to understand

$picture = ( 'artist2-1_thumb.jpg',
             'artist2-2.jpg' ,
             'artist2-3_thumb.jpg',
             'artist2-4.jpg',
             'artist2-5_thumb.jpg');

Now I want to use substr to get a new array that only has a thumb, to have a new array like this

$picturethumbs = ( 'artist2-1_thumb.jpg',
                   'artist2-3_thumb.jpg',
                   'artist2-5_thumb.jpg');

Can any substrate, but where to start?

+4
source share
2 answers

You can use array_filter()to filter an array, returning only elements that match the specified condition:

$picturethumbs = array_filter($picture, function($v) {
  return stristr($v, '_thumb'); 
});

, _thumb. , _thumb -, (, my_thumb.gif )

$picturethumbs = array_filter($picture, function($v) {
  return substr($v, -10) === '_thumb.jpg'; 
});

, 10 _thumb.jpg.

( ):

array
  0 => string 'artist2-1_thumb.jpg' (length=19)
  2 => string 'artist2-3_thumb.jpg' (length=19)
  4 => string 'artist2-5_thumb.jpg' (length=19)

+7

:

$picturethumbs = array();

foreach ($picture as $val) {
  if (substr($val, -10) == '_thumb.jpg') {
    $picturethumbs[] = $val;
  }
}
+2

All Articles