2012-02-26 17 views
0

如何在保存文件名的同時抓取其擴展名? 我爆炸字符串以獲取文件擴展名,但文件名內的所有點也消失。我的代碼遺址文件名

$description = ''; 
    $file = "Another Magazine. photos by joe smith. girl_brunette_headband.jpg"; 
    $file = explode('.',$file); 
    $extension = end($file); 

    $num_rows = count($file); 
    $j=0; 
    while($j<=$num_rows){ 
     $description .= $file[$j]; 
     $description = preg_replace("/$extension/", '', $description); 
     $j++; 
    } 

    echo $description;// outputs: Another Magazine photos by joe smith girl_brunette_headband 

我想要的輸出是:另一個雜誌。照片由喬史密斯。 girl_brunette_headband

+0

另一個需要考慮的案例是:'gift.gif'。用你當前的代碼,它會返回't'。 – icktoofay 2012-02-26 02:11:30

+0

它不是爆炸遺址文件名,你知道。 – 2012-02-26 02:19:09

+0

如果沒有爆炸,那麼什麼是刪除點? – 2012-02-26 02:34:22

回答

3

要獲得(也就是基名瓦特/ O型的擴展),你可以使用pathinfo­Docs功能:

$description = pathinfo($file, PATHINFO_FILENAME); 

要獲得它的相似之處:

$ext = pathinfo($file, PATHINFO_EXTENSION); 
0

EHM echo substr($file, 0, strrpos($file, '.'));

+0

它不給擴展。而是文件名。 – 2012-02-26 02:20:14

+0

價格: > >我想輸出的是:另一本雜誌。照片由喬史密斯。 > girl_brunette_headband – Alex 2012-02-26 02:23:29