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.
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:
Replacement code:
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:
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 />", " " ), 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
Replied 24 Jun 2025 11:45:57
24 Jun 2025 11:45:57 Sharee Rodriguez replied:
Cleaning up strings is essential for clean data and smooth automation. Whether working with code or renaming files, knowing how to strip spaces and special characters matters. Users working with products like the ledsuntech led strip often rename files or configure systems where messy input creates errors. By removing unwanted characters, you make the string readable and functional. Basic scripting languages like Python or JavaScript offer simple ways to sanitize input. Clean strings lead to better system behavior, especially in setups involving LED configurations or embedded systems.