PHP Encoding Dilemma

Discussion in 'PHP' started by instant87, Dec 27, 2010.

  1. #1
    I'm not sure how many responses this will get... but I need some expert PHP help... or just somebody who may think differently then I currently am.

    I have a string of text, that I need inserted into an iframe URL. No problem there. However, I need to find an efficient way to send special characters like & " ', etc.

    So let's pretend this is my string of text, and the echo to place it in the iframe:
    
    $sendurl =  'https://somesite.com/?listname=' . $listname . '&message=' . $message;
    echo '<iframe src="' . $sendurl . '" width="600" height="300">';
    
    PHP:
    Situation #1: A user inserts an & symbol into the message field.
    Outcome: No problem with the iframe handling it... but when somesite.com processes it, it will thing that the & sign starts a new argument (especially if I have arguments after message), and the text after it will not be included in the message argument.
    Possible Solution: Although I don't want to do it, I could just replace the & symbol with text, but I would prefer not to do that.

    Situation #2: A user places a " into the message field.
    Outcome: When inserted into the iframe url, it will break the src argument. No, the htmlspecialchars() function won't work, because then we're back to Situation #1.

    Your Mission: Come up with a solution to resolve both problems. All I have to offer is some cookies, and a cake (with chocolate icing).
     
    instant87, Dec 27, 2010 IP
  2. AssistantX

    AssistantX Peon

    Messages:
    173
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP function rawurlencode() should be able to deal with both situation.
     
    AssistantX, Dec 27, 2010 IP
    instant87 likes this.
  3. instant87

    instant87 Active Member

    Messages:
    626
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks! Worked like a charm.
     
    instant87, Dec 28, 2010 IP