2013-01-25 41 views
-1

刪除自定義的數字我有一些字符串像從字符串在PHP

$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"; //where 21313 is an id that want to be removed also. 

我想打一個函數,將刪除除一年所有的數字,而汽車「模型」。 我不知道如何檢測數字是一年還是型號或ID。 謝謝

+0

是* .JPG之前總是ID *?並在最後一個連字符後? – 2013-01-25 02:13:19

+0

一些研究進展很長。 – Kermit

+4

在嘗試創建此功能之前,首先您必須對文件進行標準命名。或者您將需要創建一個非常複雜的功能 – tomexsans

回答

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

我想你可以刪除從19和20開始的所有4位數字(如果汽車不超過2000年,只有20個)和字符串中最後一個點之前的數字(如果有的話)。 ..但是誰知道它是否會100%準確地提供您提供的信息(缺乏命名模式)。