$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>";
}