7

I have a photo gallery with a simple ul list of categories. The categories are folders on my server. If a folder is named "Ödla" then the category is named "Ödla" too. Is it possible to replace "Ö" in "Ödla" with "o" only on the category and not the actual folder on the server. I don´t want to use mysql.

I hope you understand what I mean.

Edit: The categorys is only textlinks

  • Ödla
  • Category 2

And in this example there are two folder on my server named "Ödla" and "Category 2"

I just want to rename the category links, not the folder.

Boo
  • 71
  • 1
  • 1
  • 3
  • Please clarify your question, is the 'category' a text which should be displayed, allowing `Ö` in the URL/input field or something else? – Lekensteyn Oct 13 '10 at 13:24
  • Possible duplicate of this question: http://stackoverflow.com/questions/1890854/how-to-replace-special-characters-with-the-ones-theyre-based-on-in-php – Dave Oct 13 '10 at 13:31

4 Answers4

16

You could always use the PHP function rename(). Using @steven_desu's str_replace method you can call the rename of the old folder with the new name. Have a look at the documentation.

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

EDIT

example :

<?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);

?>

Please just check me on this?

Etienne Marais
  • 1,660
  • 1
  • 22
  • 40
2

Assuming you've already gotten a list of folders on the server (via while() loop or glob() function) and this list is stored in $folderList[], I'd try something like the following if you're just outputting the result:

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

It's fairly sloppy, but it's also fairly simple. If you wanted something that would run quicker you'd be looking at doing math or comparisons with the binary value of the unicode. For instance, everything in the $o array is the unicode characters 00D2 to 00D6 and 00F2 to 00F6. So if the letter is between dechex('00D2') and dechex('00D6') OR that letter is between dechex('00F2') and dechex('00F6') then replace it with "o".

If you're getting the value without the special characters (such as via URL post) and you want to map it to a folder, then you have to take a slightly different approach. First, realize that this isn't the ideal solution since you may have two folders, one named Òdla, one named Ödla. In this case, the search phrase odla could only refer to one of these folders. The other would be permanently ignored. Assuming you're fine with this (such as: you can guarantee that there will be no such duplicate folder names), you would probably want to use 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);
?>
stevendesu
  • 15,753
  • 22
  • 105
  • 182
  • And if I use your example there wouldn´t be any problems finding the images in the folders? I tried something like that, but "odla" couldn´t find the images in "Ödla" – Boo Oct 13 '10 at 13:40
  • You should only be changing the name that is displayed, not where the link is pointing to e.g. Olda – Dave Oct 13 '10 at 14:13
  • The name that is displayed is the same as the link. The category name is shown in the URL e.g http://domain.com/index.php?cat=Ödla, I want it to be ?cat=odla – Boo Oct 13 '10 at 14:18
  • @Boo Ah. I understand the problem now. I thought you just needed to display a different name after all parsing was done. I didn't realize it was getting sent as part of a URL. I'll update my answer. – stevendesu Oct 15 '10 at 00:36
2

You might want to look at iconv, Another stack question

Community
  • 1
  • 1
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
1

I read what @steven_desu wrote and think it looks interesting, but I´m not sure how to use it. I´ve tried with this:

<?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>";
?>
Boo
  • 11
  • 2