ereg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

ereg_replace

Post 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 ?
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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;
Post Reply