http://qs321.pair.com?node_id=9624

rodry has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that will construct a link to another PERL script and pass it parameters based on a column returned by a query.
There is a problem however. This column that I am passing as a parameter may or may not have spaces in it. More than likely it will have spaces.
So my question is: how do I detect this spaces and replace them with the appropiate ASCII character code.
As an alternative, how do I filter the user input when providing values for this column so that it will eliminate the spaces, if any?
Thanks in advance

Originally posted as a Categorized Question.

  • Comment on When constructing a URL from script variables, how do I handle spaces properly?

Replies are listed 'Best First'.
Re: When constructing a URL from script variables, how do I handle spaces properly?
by merlyn (Sage) on Apr 30, 2000 at 16:01 UTC
    In fact, if you are trying to construct
    <a href="/cgi-bin/editmarine.pl?marine=$marineid">edit marine</a>
    You'll run into a lot of problems if $marine is not URI-encoded. So the first step is to trot over to URI::Escape and run it through there. No spaces will be left!

    Editor's note:

    #Sample Code my $SafeMarine = URI::Escape::uri_escape($marineid); <a href="/cgi-bin/editmarine.pl?marine=$SafeMarine">edit marine</a>
      I've also used the code below to replace spaces with the correct chars.
      $value =~ s/\s/\%20/g;
      And it seems to work good for my purposes.
Re: When constructing a URL from script variables, how do I handle spaces properly?
by Accipiter (Initiate) on Jul 18, 2001 at 01:08 UTC
    You might want to try modifying it via a substitution. Something like:

    $boomdiggity = "www.yayforperl.org/index.pl?go=foo bar"

    Then run $boomdiggity through "s/ /%20"

    Originally posted as a Categorized Answer.