$str1 = "Audi-TT-RS-2012-widescreen-77.jpg";
$str2 = "Audi-R8-widescreen-039.jpg";
$str3 = "2008-bmw-6-series-front-and-side.jpg";
$str4 = "Opel_tigra_2-doors-21313.jpg";
$strs = array($str1, $str2, $str3, $str4);
foreach ($strs as $str) {
preg_match_all('/(?<!\d)(\d{4})(?!\d)\-*/', $str, $matches);
foreach ($matches[1] as $year) {
if (abs($year - date("Y")) < 100) {
// this is within 100 years
}
}
}
刪除號碼
$str1 = "Audi-TT-RS-2012-widescreen-77.jpg";
$str2 = "Audi-R8-widescreen-039.jpg";
$str3 = "2008-bmw-6-series-front-and-side.jpg";
$str4 = "Opel_tigra_2-doors-21313.jpg";
$strs = array($str1, $str2, $str3, $str4);
foreach ($strs as $str) {
preg_match_all('/(?<!\d)(\d{4})(?!\d)\-*/', $str, $matches);
foreach ($matches[1] as $year) {
if (abs($year - date("Y")) < 100) {
$str = preg_replace('/(?<!\d)(\d{4})(?!\d)/', '[year]', $str);
}
}
preg_match_all('/(?<!\d)(\d{1})(?!\d)\-*/', $str, $matches);
foreach ($matches[1] as $model) {
$str = preg_replace('/(?<!\d)(\d{1})(?!\d)/', '[model]', $str);
}
preg_match_all('/\-*(\d+)\-*/', $str, $matches);
foreach ($matches[1] as $id) {
$str = preg_replace('/\-*(\d+)\-*/', '', $str);
}
$str = preg_replace('/\[year\]/', $year, $str);
$str = preg_replace('/\[model\]/', $model, $str);
echo $str."<br>";
}
是* .JPG之前總是ID *?並在最後一個連字符後? – 2013-01-25 02:13:19
一些研究進展很長。 – Kermit
在嘗試創建此功能之前,首先您必須對文件進行標準命名。或者您將需要創建一個非常複雜的功能 – tomexsans