HOW to change + to %2B in url

Discussion in 'Programming' started by ki3000, Nov 28, 2009.

  1. #1
    Hi, I am having more troubles with my site. I have a job site, and when people search for jobs or a city with two words (like security guard, or new york), the results don't come out right. For example, it will search for security and guard separately, just as it would with new, and then york. So the url comes out like this: http :// ... ... com / ... /security+guard...new+york... instead of
    this: http :// ... ... com / ... /security%2Bguard...new%2Byork.

    Does any1 know how i can fix this and make it look like the second one. I can also show or paste (it here) the php results file if you think u can help.

    Thanks........
     
    ki3000, Nov 28, 2009 IP
  2. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #2
    use str_replace function before you pass the search string to search result processing page or submitting the form.

    See a sample code below.

    
    $signs = array("+", " ");
    $words = array("%2B", "%20");
    $searchtext = "security guard+new york";
    $change = str_replace($signs, $words, $searchtext);
    Print $change; 
    
    Code (markup):
    Now use the $change to pass the search text value. Hope this helps!
     
    spc, Nov 28, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is due to them entering a space and forms converting the spaces to +. If they enter New+York then it will indeed put New%2BYork. If you want to do it with php use urlencode()
     
    JAY6390, Nov 28, 2009 IP
  4. rogan4567

    rogan4567 Active Member

    Messages:
    103
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    55
    #4
    I don't know PHP, but this regex will work:

    
    s/\+/\%2B/g; 
    
    Code (markup):
    I'm not sure how it works in PHP, but in Perl, you just need to use the variable name like this:

    $var = "http://www.example.com/?new+york+haircut";

    $var =~ s/\+/\%2B/g;

    Result = http://www.example.com/?new+york+haircut

    It would be the same if the search query were its own variable (instead of the entire URL).
     
    rogan4567, Nov 29, 2009 IP