Boo
Boo

Reputation: 71

PHP rename folder

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

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.

Upvotes: 7

Views: 47848

Answers (4)

Boo
Boo

Reputation: 11

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

Upvotes: 1

stevendesu
stevendesu

Reputation: 16771

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

Upvotes: 2

Etienne Marais
Etienne Marais

Reputation: 1690

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?

Upvotes: 16

Phill Pafford
Phill Pafford

Reputation: 85298

You might want to look at iconv, Another stack question

Upvotes: 2

Related Questions