2010-10-13 212 views
4

我有一個簡單的ul類別列表的照片庫。 類別是我的服務器上的文件夾。 如果一個文件夾被命名爲「Ödla」,那麼該類別也被命名爲「Ödla」。 是否可以將「Ödla」中的「Ö」替換爲「o」而不是服務器上的實際文件夾。我不想使用mysql。PHP重命名文件夾

我希望你明白我的意思。

編輯:的categorys只textlinks

  • ODLA
  • 類別2

而且在這個例子中有我命名爲 「ODLA」 和 「類別2」

服務器上的兩個文件夾

我只想重命名類別鏈接,而不是文件夾。

+0

請澄清一下您的問題,是'category'是應該顯示的文本,允許在URL /輸入字段或其他內容中使用'Ö'? – Lekensteyn 2010-10-13 13:24:38

+0

這個問題可能的重複:http://stackoverflow.com/questions/1890854/how-to-replace-special-characters-with-the-ones-theyre-based-on-in-php – Dave 2010-10-13 13:31:02

回答

0

假設你已經得到的文件夾列表在服務器上(通過while()循環或​​3210功能),該列表存儲在$folderList[],我會嘗試像如果你只是把結果如下:

$a = array(...); 
$e = array(...); 
$i = array(...); 
$o = array('Ò','Ó','Ô','Õ','Ö','ò','ó','ô','õ','ö'); 
$u = array(...); 
foreach($folderList as $folder){ 
    $folder = str_replace($a,"a",$folder); 
    $folder = str_replace($e,"e",$folder); 
    $folder = str_replace($i,"i",$folder); 
    $folder = str_replace($o,"o",$folder); 
    $folder = str_replace($u,"u",$folder); 
} 

這是相當馬虎,但它也相當簡單。如果你想要更快運行的東西,你可以考慮用unicode的二進制值進行數學或比較。例如,$o陣列中的所有內容都是unicode字符00D2至00D6和00F2至00F6。因此,如果該字母在dechex('00D2')dechex('00D6')之間或該字母在dechex('00F2')dechex('00F6')之間,則將其替換爲「o」。

如果您獲取的值沒有特殊字符(例如通過URL發佈)並且您想將其映射到文件夾,那麼您必須採取稍微不同的方法。首先,要意識到這不是理想的解決方案,因爲您可能有兩個文件夾,一個名爲Òdla,一個名稱爲Ödla。在這種情況下,搜索短語odla只能引用這些文件夾中的一個。另一個將被永久忽略。假設你沒有問題(比如:你可以保證不會有這樣的重複文件夾名稱),你可能會想要使用GLOB_BRACE

<?php 
    $vowels = array("a", "e", "i", "o", "u"); 
    $replace = array("...", "...", "...", "{Ò,Ó,Ô,Õ,Ö,ò,ó,ô,õ,ö}", "..."); 

    $search = "odla"; 
    $search = str_replace($vowels, $replace, $search); 

    // Returns every folder who matches "Òdla", "Ódla", "Ôdla".... 
    glob($search, GLOB_BRACE); 
?> 
+0

如果我用你的例如,在文件夾中找到圖像沒有任何問題?我試過類似的東西,但「odla」無法找到「Ödla」中的圖像 – Boo 2010-10-13 13:40:06

+0

您應該只更改顯示的名稱,而不是鏈接指向的位置,例如, Olda Dave 2010-10-13 14:13:19

+0

顯示的名稱與鏈接相同。類別名稱顯示在URL中,例如http://domain.com/index.php?cat=Ödla,我希望它是?cat = odla – Boo 2010-10-13 14:18:04

8

你總是可以使用PHP函數rename()。使用@ steven_desu的str_replace方法,您可以使用新名稱調用舊文件夾的重命名。看看文檔。

http://www.php.net/manual/en/function.rename.php

編輯

例如:

<?php 
// Create arrays with special chars 
$o = array('Ò','Ó','Ô','Õ','Ö','ò','ó','ô','õ','ö'); 
// Remember to remove the slash at the end otherwise it will not work 
$oldname = '/path/to/directory/Ödla'; 

// Get the directory name 
$old_dir_name = substr($oldname, strrpos($oldname, '/') + 1); 

// Replace any special chars with your choice 
$new_dir_name = str_replace($o, 'O', $old_dir_name); 

// Define the new directory 
$newname = '/path/to/new_directory/' . $new_dir_name; 

// Renames the directory 
rename($oldname, $newname); 

?> 

請只檢查我在這?

0

我讀了@steven_desu寫的,認爲它看起來很有趣,但我不確定如何使用它。我試過這個:

<?php 
function fixlink($link){ 
    $match = array('Å','Ä','Ö','å','ä','ö',' '); 
    $replace = array('a','a','o','a','a','o','-'); 
    return str_replace($match, $replace, $link); 
} 
function matchlink($search){ 
    $vowels = array("a", "e", "i", "o", "u"); 
    $replace = array("...", "...", "...", "{Ò,Ó,Ô,Õ,Ö,ò,ó,ô,õ,ö}", "..."); 

    $search = "odla"; 
    $search = str_replace($vowels, $replace, $search); 

    // Returns every folder who matches "Òdla", "Ódla", "Ôdla".... 
    glob($search, GLOB_BRACE); 
} 
echo "<a href=\"index.php?page=".matchlink(fixlink($file))."\">".$file."</a>"; 
?>