Gossamer Forum
Home : General : Perl Programming :

Replace blank space with '_'

Quote Reply
Replace blank space with '_'
Hi,

I'm using Links SQL and have a custom script that has the following lines:

{
$clean_name = $row->{'Full_Name'};
$url = qq~$CFG->{build_root_url}/$clean_name/$CFG->{build_index}~;
}

The variable Full_Name comes from the Links SQL Category table, and my problem are the cases where the Full_Name value has spaces. For example:

Category/Sub Category/Another Sub Category

I somehow need it to take the $clean_name variable and then replace any blank spaces with an underscore character so it would be:

Category/Sub_Category/Another_Sub_Category

Does anyone know of a line I could add to the above code that would accomplish this?

--FrankM

Last edited by:

FrankM: Apr 12, 2004, 2:32 PM
Quote Reply
Re: [FrankM] Replace blank space with '_' In reply to
I added the line in red which seemed to work:

{
$clean_name = $row->{'Full_Name'};
$clean_name =~ s/ /_/;
$url = qq~$CFG->{build_root_url}/$clean_name/$CFG->{build_index}~;
}

--FrankM
Quote Reply
Re: [FrankM] Replace blank space with '_' In reply to
You may want to add a 'g' in there.. .i.e;

Code:
$clean_name =~ s/ /_/g;

This will make the change 'globally' in the variable (your version would only do the first occurence).

Hope that helps :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Replace blank space with '_' In reply to
Thanks Andy, you were right, I did need the 'g' in there.