Jump to content

[SOLVED] How do I print the spaces between strings (%20) in variables


poleposters

Recommended Posts

I want to embed a dynamic google map on my site that shows the address of the registered user.

 

The code I have works great except I have discovered a flaw. If the name of the street or suburb has a double barrel name. ie 'beverly hills' or 'old canterbury' rd the code gets muddled up and the map doesn't display.

 

 

            This is the code that needs to be printed in HTML

           

address=261%20old%20canterbury%20rd%20dulwich%20hill%

 

            This is the code with variables.

           

             $number=261
             $street=old canterbury rd
             $suburb=dulwich hill


             address=$number%20$street%20$suburb%

 

            And this is the output

           

address=261%20old  canterbury rd%20dulwich hill%

 

As you can see the spaces in between the words are printed which seems to confuse  the mapping script. The variables work fine for single names. Is there a way to pull the variables from the database and convert the spaces to %20?

 

Use urlencode() when encoding a string to be used in an URL.

 

<?php
$number = '261';
$street = 'old canterbury rd';
$suburb = 'dulwich hill';
$address = urlencode($number.' '.$street.' '.$suburb);
?>

 

That will take care of any character that could mess up the URL.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.