2017-02-14 73 views
0

我想在這裏做什麼,在我的PHP代碼下面我必須手動設置文件名,我想使它一些如何抓住文件名自動但沒有文件擴展獲取文件名沒有文件擴展名在我的PHP代碼

此處,我想文件名

$Pages = array(
    'clothes' => 'Clothes', 
    'shirt' => 'shirt', 
    'this-shirt' => 'This Shirt' 
); 

它說:「這個恤」是文件名,我希望它自動設置的,而不是我寫下來,我的代碼部分每次我創建一個頁面。這裏的完整代碼

<?php 
$Pages = array(
    'clothes' => 'Clothes', 
    'shirt' => 'shirt', 
    'this-shirt' => 'This Shirt' 
); 

$path = $_SERVER["PHP_SELF"]; 
$parts = explode('/', $path); 
if (count($parts) < 2) { 
    echo("home"); 
} else { 
    echo ("<a href=\"http://domain.com\">Home</a> &raquo; "); 
    for ($i = 2; $i < count($parts); $i++) { 
     if (!strstr($parts[$i], ".")) { 
      echo("<a href=\""); 
      for ($j = 0; $j <= $i; $j++) { 
       echo $parts[$j] . "/"; 
      }; 
      echo("\">" . str_replace('-', ' ', $Pages[$parts[$i]]) . "</a> &raquo; "); 
     } else { 
      $str = $parts[$i]; 
      $pos = strrpos($str, "."); 
      $parts[$i] = substr($str, 0, $pos); 
      echo str_replace('-', ' ', $Pages[$parts[$i]]); 
     } 
    } 
} 

希望你明白了。感謝

+0

'$文件= '這-shirt.pdf'; $ filename =(count(explode('。',$ file))=== 1?$ file:implode('。',array_slice(explode('。',$ file),0,(count(explode(' 。',$ file)) - 1)))); echo $ filename;' – MonkeyZeus

+0

@MonkeyZeus親愛的你可以請發表你的代碼在答案?因爲它不是這樣清楚的 –

+0

哪個變量包含帶有**擴展名的「this-shirt **」? – MonkeyZeus

回答

1

這應做到:

// get this-shirt.php from the URL 
$file = basename($_SERVER["PHP_SELF"]); 

// pure magic 
$filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1)))); 

$Pages = array(
    'clothes' => 'Clothes', 
    'shirt' => 'shirt', 
    $filename => 'This Shirt' // use $filename to declare the array's key 
); 
+0

工作兄弟。完全像我想要的一樣。非常感謝你 –

+0

親愛的你可以用這個幫助我嗎http://stackoverflow.com/questions/42262290/need-help-in-my-php-code-getting-wrong-directory-on-breadcrumb –