Pure PHP Upload 2 Support Product Page

strip spaces and special charcters

Asked 26 Oct 2004 13:34:38
1
has this question
26 Oct 2004 13:34:38 Paul Maher posted:
Any way to strip out spaces and special chracters from filnames ?


Thanks.......

Replies

Replied 10 Jan 2011 04:25:24
10 Jan 2011 04:25:24 Bart Garner replied:
Is there any way to STOP the code from replacing spaces with _?

I know all us web types know to not use spaces but tell that to the average user.

The truth is, a space in the filename renders just fine in todays browsers so it is a bother to tell people they have to rename their files.
Replied 21 Jan 2011 18:38:58
21 Jan 2011 18:38:58 Gary Block replied:
When users upload files, spaces are replaced with an underscore in the actual file name but the file name that's saved in the database still contains spaces. Therefore the two don't match when retrieving a recordset. To solve this problem I use the following code on the webpage to replace spaces with underscores.

Original code:


<?php echo $row_Recordset1['myfilename']; ?>



Replacement code:


<?php 
$data = $row_Recordset1['myfilename'];
echo str_replace(
     array(" "),
     array("_"),
     $data
) ; ?>



You could modify the code to replace several different characters. The first array row contains the original characters and the second array row contains the character that should replace the matching character above, for example:


<?php 
$data =  $row_Recordset1['myfilename'];
echo str_replace(
    array(" ", "<br />", "&nbsp;" ),
    array("_", "&", "-" ),
    $data
) ; ?>





Edited by - Gary B on 21 Jan 2011  18:39:50


Edited by - Gary B on 21 Jan 2011  18:42:09


Edited by - Gary B on 21 Jan 2011  18:44:43

Reply to this topic