Need help... convert '%20' -> '+'... is this possible?

Discussion in 'PHP' started by cgo85, Apr 1, 2008.

  1. #1
    I've been trying to figure this out all day. Searching for possible rewrites, ANYTHING, but to no avail. This script works just fine when the city is a one word city such as 'Seattle'. However, When it's something like 'Los Angeles' I get error signs. I know that this is being cause because the city is being called from the URL and comes up as 'Los%20Angeles'. Is there anyway within this code that I code convert a '%20' to a '+'?


    <?php 
    
      $city = $_GET['city'];
      
    	    $request = 'http://local.yahooapis.com/MapsService/V1/mapImage?appid=<my_id>&image_height=103&image_width=194&city=' . $city . '&state=' . $abbr . '&zoom=4&radius=10&output=php';
    	    
    	    $response = file_get_contents($request);
    	
    	    if ($response === false) {
    	    	die('sorry');
    	    }
    	
    	    $phpobj = unserialize($response);
    	
    	    echo '<img src="'.$phpobj["Result"].'">';
    	?>
    PHP:

     
    cgo85, Apr 1, 2008 IP
  2. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes you can simply use like that :
    <?php
    $city = $_GET['city'];
    $city = str_replace("%20","+",$city );

    or there be also inbuild function to use this for url encode and decode......
     
    singh.ajit05, Apr 1, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    
    string urldecode  ( string $str  )
    
    PHP:
    Peace,
     
    Barti1987, Apr 1, 2008 IP
  4. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    singh, I've tried that before and it didn't work. Still returns an error message.

    Azizny, I'm not sure I really understand how to use that. Within that code, am I suppose to substitute something there with '$city'?
     
    cgo85, Apr 1, 2008 IP
  5. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Strange that it returns an error message. Can you post it here?

    Otherwise, you could try with preg_replace instead of str_replace:
    $city = $_GET['city'];
    $city = preg_replace('%20', '+', $city);
     
    elias_sorensen, Apr 1, 2008 IP
  6. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this:
    $city = urldecode($_GET['city']);
     
    tamilsoft, Apr 1, 2008 IP
  7. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Let me know if problem was not solved...
     
    singh.ajit05, Apr 2, 2008 IP
  8. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    elias, when I tried that it showed the map but starred location was not in right place AND gave this message:

    Warning: preg_replace() [function.preg-replace]: No ending delimiter '%' found in /home/coyoloko/public_html/yahoo_api.php on line 4

    tamilsoft, with that I just get this error:

    Warning: file_get_contents(http://local.yahooapis.com/MapsServ...4-&image_height=103&image_width=194&city=Palm Springs&state=CA&zoom=4&radius=10&output=php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/coyoloko/public_html/yahoo_api.php on line 7


    I really appreciate everyone's help. Hopefully I can get this figured out.
     
    cgo85, Apr 2, 2008 IP