Page 1 of 1

ereg_replace

Posted: Thu Nov 11, 2004 1:45 pm
by Shendemiar
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 ?

Posted: Thu Nov 11, 2004 2:53 pm
by Shendemiar
Here's what i have, but i doesn't replace anything...

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 = "/^&#1111;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;

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

Posted: Thu Nov 11, 2004 3:07 pm
by Shendemiar
Got it, thanks for the help! :D

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 = "/&#1111;^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)
&#123;
$REG = "/&#1111;^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;
&#125;