1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Removing spaces from get variables

Discussion in 'PHP & Perl' started by mightybh, Jun 10, 2012.

  1. mightybh

    mightybh Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 27, 2008
    Messages:
    1,029
    Likes Received:
    1,714
    Occupation:
    CEO
    Location:
    UK
    A simple get variable like this

    <?php echo $_GET["var"]; ?>

    When I go to my url something.php?var=this is a test

    %20 is added to replace the spaces.

    End result echos: this is a test

    How do I remove the spaces completely so that I end up with: thisisatest? Any ideas?

    I tried trim and $sometext = preg_replace('/\s+/', ' ', $sometext); but can't combine the two together.

    Thanks!
     
  2. sirgold

    sirgold Supreme Member

    Joined:
    Jun 25, 2010
    Messages:
    1,260
    Likes Received:
    645
    Occupation:
    Busy proving the Pareto principle right
    Location:
    A hot one
    str_replace(' ', '', $string); // good ol' str_replace? ;)
     
    • Thanks Thanks x 1
  3. mightybh

    mightybh Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 27, 2008
    Messages:
    1,029
    Likes Received:
    1,714
    Occupation:
    CEO
    Location:
    UK
    Thanks but how would I add that into <?php echo $_GET["var"]; ?> ?
     
  4. bastienvans

    bastienvans Newbie

    Joined:
    May 3, 2009
    Messages:
    20
    Likes Received:
    2
    Encode your url.

    <?php

    if(isset($_GET["var")){

    $var = $_GET["var"];
    $var = urlencode($var);

    echo $var;

    }

    ?>

    If this doesn't work for your application, you can go ahead with str_replace.

    <?php

    if(isset($_GET["var")){

    $var = $_GET["var"];
    $var = str_replace(' ','',$var);

    echo $var;

    }
     
    • Thanks Thanks x 1
    Last edited: Jun 10, 2012
  5. heroix

    heroix Newbie

    Joined:
    Jun 14, 2012
    Messages:
    2
    Likes Received:
    0
    str_replace(' ', '', $_GET["var"]);
    Does not work?
     
  6. labsdesigns

    labsdesigns Newbie

    Joined:
    Jun 7, 2012
    Messages:
    11
    Likes Received:
    2
    use url encoded..that to cleanup
     
  7. TheGodWings

    TheGodWings Newbie

    Joined:
    Jun 27, 2012
    Messages:
    16
    Likes Received:
    17
    Location:
    India
    Home Page:
    echo str_replace('%20', '', $_GET["var"]);


    This will work...