Gossamer Forum
Home : Products : DBMan : Customization :

String Manipulation

Quote Reply
String Manipulation
Lets say a record returned by DBMan is "Black Hat"

I need to place this in a query string at times, which means I need the string to be "Black+Hat"

How can I acheive this?

Thanks in advance.
Quote Reply
Re: String Manipulation In reply to
If the only character you're going to need to convert is a space, you can use

$rec{'Fieldname'} = s/ /+/g;

just before you are going to print out the URL that includes $rec{'Fieldname'}.

If you're going to have other characters that might cause problems (I'm not completely certain what they are, but I'd be hesitant about anything other than numbers and letters) you can use a subroutine to convert them all.

Code:
sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
return $toencode;
}

Put the above somewhere in db.cgi and then to use it,

$rec{'Fieldname'} = &urlencode($rec{'Fieldname'});

Again, you'll want to do this just before you print out the url.

------------------
JPD





Quote Reply
Re: String Manipulation In reply to
Thanks JPD

This will definitely help

Great Support Forum!!!