CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Substitution

 



Pro_4
User

May 9, 2001, 1:59 PM

Post #1 of 6 (971 views)
Substitution Can't Post

Hey,
How do you subsititute all white spaces in a filename with _ . The reason why is because after it is uploaded when i try to link to it the link doesnt want to work because of the spaces. I attached the script that views my database and prints out the files and links to them.

Thanks :)

People Engrossed by a Reliable Language


wickedxter
User

May 9, 2001, 4:26 PM

Post #2 of 6 (964 views)
Re: Substitution [In reply to] Can't Post

try:

$string =~ s/ /_/g;

This replaces every space with an under score.



rjoseph
Novice

May 9, 2001, 4:33 PM

Post #3 of 6 (964 views)
Re: Substitution [In reply to] Can't Post

Use the transliteration operator - tr/SEARCHLIST/REPLACELIST/
It simply searches character by character for something from the SEARCHLIST, and if found, replaces it with the corresponding character in REPLACELIST. Some people may say use the s/// operator, but because that uses regexs, for this case it would be much slower.

Code
$filename =~ tr/ /_/;

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


freddo
User

May 10, 2001, 9:38 AM

Post #4 of 6 (956 views)
Re: Substitution [In reply to] Can't Post

Hi Pro4,

there's probably no need to change the filenames, but when your program print the url replace spaces into %20 like:

Code
$url = $filename = "a file with spaces.mp3"; 
$url =~ s/ /%20/g;
print "<a href=\"$url\">$filename</a>\n";

will print:
<a href="a%20file%20with%20spaces.mp3">a file with spaces.mp3</a>

and it will work...

i hope this helps
freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?

(This post was edited by freddo on May 10, 2001, 8:41 AM)


rjoseph
Novice

May 10, 2001, 12:39 PM

Post #5 of 6 (952 views)
book.gif [In reply to] Can't Post

Not only do some older browsers have trouble with this scheme of URL encoding, but files names with spaces in them are not cross-platform compatible - many older *NIX type OSes do not allow any spaces in the name of a file, and you want your visitors to your site to be able to download these files without having to worry about this. Although you are right, it is sometimes just a better idea to play it as safe as possible, especially on the web.

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 10, 2001, 12:50 PM

Post #6 of 6 (951 views)
Re: book.gif [In reply to] Can't Post

Wow thanks a lot guys that did the trick.

:)

People Engrossed by a Reliable Language

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives