Mastering PHP: Handling spaces in passed values

When I try to pass to a separate web page a value that has a space, that called web page truncates the value from the instance of the space. Of course, the solution is to replace the space with “%20”. But the problem is that the value being passed comes from another variable (say from an item in a database table’s row). There must be a way to replace all instances of space in each variable’s value.

Solution: Use the str_replace function to replace all instances of space with ‘%20’.

Advertisements

One comment

  1. Other ways worth considering are:

    $var = ‘hello world ‘;
    $var1 = preg_replace(‘/\s/’, ‘%20’, $var);

    //$var1 should contain “hello%20world%20%20”

    $var2 = urlencode($var);

    //$var2 should contain “hello+world++”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s