从string PHP结束SUBSTR? [英] Substr from end of string php?
问题描述
我有这种类型的数组,我会变得非常简单易懂。
I have this kind of array, i will make it very simple to understand
$picture = ( 'artist2-1_thumb.jpg',
'artist2-2.jpg' ,
'artist2-3_thumb.jpg',
'artist2-4.jpg',
'artist2-5_thumb.jpg');
现在我想用SUBSTR获得新的数组只有拇指,能有新的数组像这样
Now i want use substr to get new array that only have thumb, to have new array like this
$picturethumbs = ( 'artist2-1_thumb.jpg',
'artist2-3_thumb.jpg',
'artist2-5_thumb.jpg');
可以将某些SUBSTR但哪里开始呢?
Can some substr but where to start?
推荐答案
您可以使用的 array_filter()
到数组进行筛选,只返回匹配给定的条件下哪些项目:
You could use array_filter()
to filter the array, returning only items which match the given condition:
$picturethumbs = array_filter($picture, function($v) {
return stristr($v, '_thumb');
});
将返回包含字符串 _thumb
所有数组项。如果你不知道该文件的扩展名这可能是有用的,或者如果 _thumb
显得比字符串的结尾(例如:其他地方my_thumb.gif
将仍然匹配)
Would return all array items which contain the string _thumb
. This could be useful if you don't know the extension of the file, or if _thumb
appears somewhere other than the end of the string (eg. my_thumb.gif
would still match)
$picturethumbs = array_filter($picture, function($v) {
return substr($v, -10) === '_thumb.jpg';
});
将返回其中最后10个字符匹配 _thumb.jpg
。
两者(给你的例子数组)输出:
Both (given your example array) output:
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)
这篇关于从string PHP结束SUBSTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!