问题描述
我真的需要一些帮助...我只是无法使其工作.
I really need some help with this... i just cant make it work.
现在我有这段代码,并且工作正常.它的作用是...根据名称中的日期根据日期重新调整目录中的所有文件.
For now i have this piece of code and it's working fine.What it does is... retuns all files within a directory according to date in their name.
<?php
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$filteredImages = [];
foreach($images as $image) {
$current_date = date("Ymd");
$file_date = substr($image, 0, 8);
if (strcmp($current_date, $file_date)>=0)
$filteredImages[] = $image;
}
echo json_encode($filteredImages, JSON_UNESCAPED_UNICODE);
?>
但是现在我需要过滤那些文件(可能在执行此代码之前).根据它们名称中的字符串.
But now i need to filter those files (probably before this code is even executed). acording to the string in their name.
文件以以下方式命名:
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.123456789.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.9.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.458.jpg
我只需要过滤掉最后一个数字串(在"."
和".jpg"
之间)中具有特定数字的那些.编号9
i need to filter out only ones that have certain number within that string of numbers at the end (between "."
and ".jpg"
) eg. number 9
$number = 9
我正在尝试使用这段代码来仅分隔名称的最后一部分:
i was trying with this piece of code to seperate only that last part of name:
<?php
function getBetween($jpgname,$start,$end){
$r = explode($start, $jpgname);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$jpgname = "yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.12789.jpg";
$start = ".";
$end = ".jpg";
$output = getBetween($jpgname,$start,$end);
echo $output;
?>
我想我在所有这些中都需要STRIPOS ...但是我现在迷路了...:(
and i guess i would need STRIPOS within all of this... but im lost now... :(
推荐答案
您可能可以使用preg_grep.
它是数组的正则表达式.
You can probably use preg_grep.
It's regex for arrays.
这未经测试,但我认为应该可以.
This is untested but I think it should work.
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$find = 9;
$filtered = preg_grep("/.*?\.\d*" . $find . "\d*\./", $images);
正则表达式将查找到一个点,然后是任何数字或没有数字,$ find再然后是任何或没有数字,再到一个点的任何东西.
The regex will look for anything to a dot then any number or no number, the $find then any or no number again and a dot again.
这篇关于我需要检查数字中是否包含某些数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!