php_begins Posted July 25, 2011 Share Posted July 25, 2011 hello. I need some help here. I have dynamic variable that contains characters separated by blank spaces. I need to replace the blank space (%20) with a plus(+) sign) wherever it occurs. For example, my variable $label might contain $label=one word. when i echo $label it prints one%20word. I would like to print it as one+word. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/242795-php-converting-blank-spaces-to-symbol/ Share on other sites More sharing options...
marcus Posted July 25, 2011 Share Posted July 25, 2011 echo str_replace(array('%20',' '),'+',$label); Quote Link to comment https://forums.phpfreaks.com/topic/242795-php-converting-blank-spaces-to-symbol/#findComment-1247005 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 mgallforevers code / response will work. However, I'm curious how you are setting those variables that have %20 in them. Can I see your code? Quote Link to comment https://forums.phpfreaks.com/topic/242795-php-converting-blank-spaces-to-symbol/#findComment-1247010 Share on other sites More sharing options...
php_begins Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks mgallforever :-) @teynon.. actually i am printing it in the url for searching purposes..so any blank space comes as %20! Quote Link to comment https://forums.phpfreaks.com/topic/242795-php-converting-blank-spaces-to-symbol/#findComment-1247020 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 That's pretty much why I asked. %20 is really only used in urls. If you are manually setting that, you are making more work for yourself. You should use urlencode on variables when sending them to the browser. http://php.net/manual/en/function.urlencode.php Then if you need to decode a url: http://www.php.net/manual/en/function.urldecode.php Although for the most part you shouldn't ever have to use urldecode. Example of urlencode: <?php $variable=urlencode("my variable data & info"); echo "<a href=\"url.php?variable={$variable}\">Go!</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242795-php-converting-blank-spaces-to-symbol/#findComment-1247024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.