Removing spaces from get variables

mightybh

Senior Member
Joined
Feb 27, 2008
Messages
1,029
Reaction score
1,734
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!
 
Thanks but how would I add that into <?php echo $_GET["var"]; ?> ?
 
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;

}
 
Last edited:
Back
Top