I need to get rid of all nonstandard characters in filenames. First i replace ' ' with %20, å's with a's, Ä's with A's and so on, but then i want to use ereg_replace to get rid of all the rest nonstandard stuff. This is ok, but:
How do i get ereg_replace to replace every non ( 0-9, a-x, A-X, _ , - ) characters with, say 'X' char ?
ereg_replace
Moderator: General Moderators
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
Here's what i have, but i doesn't replace anything...
Outputs:
kala1,p:al;Öa2KISSA4~KA^LA%20%20%20%20Ökana.doc
kala1,p:al;Öa2KISSA4~KA^LA%20%20%20%20Ökana.doc
Code: Select all
<?php
// Idiotic filename i have to deal with
$A = "kala1,p:al;Öa2KISSA4~KA^LA%20 Ökana.doc";
$B = str_replace(" ","%20",$A);
$reg = "/^їa-zA-Z0-9\_\-\%'']";
$reg .= "$/";
// Get rid of all nonstandard chars, mark the spot with X
$C = ereg_replace($reg,"X",$B);
echo $C;
echo "<br><br>";
// Get rid of all nonstandard chars, mark the spot with X
$C = preg_replace($reg,"X",$B);
echo $C;
?>kala1,p:al;Öa2KISSA4~KA^LA%20%20%20%20Ökana.doc
kala1,p:al;Öa2KISSA4~KA^LA%20%20%20%20Ökana.doc
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
Got it, thanks for the help!
That later replace does it right!
Tidy version:
That later replace does it right!
Code: Select all
<?php
// Idiotic filename i have to deal with
$A = "kala1,p:al;Öa2KISSA4~KA^LA%20 Ökana.doc";
$B = str_replace(" ","%20",$A);
$reg = "/ї^a-zA-Z0-9_.\-\%'']/";
// Get rid of all nonstandard chars, mark the spot with X
$C = ereg_replace($reg,"X",$B);
echo $C;
echo "<br><br>";
// Get rid of all nonstandard chars, mark the spot with X
$C = preg_replace($reg,"X",$B);
echo $C;
?>Tidy version:
Code: Select all
function siivoa_nimi($nimi)
{
$REG = "/ї^a-zA-Z0-9_.\-\%'']/";
$SF = array(" ", "ä", "Ä", "å", "Å", "ö", "Ö");
$RW = array("%20", "a", "A", "a", "A", "o", "O");
$Apu = str_replace($SF, $RW, $nimi);
$Apu = preg_replace($REG,"X",$Apu);
return $Apu;
}