Problem with '&' charachter.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zamuel
    New Member
    • Sep 2006
    • 42

    Problem with '&' charachter.

    Hey.

    I have a problem with "&" character. When I try to send a mail from web application and I include '&' to the post the whole message shows empty when send it through. So what can I do to prevent this?

    Thanks in advance!
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by zamuel
    Hey.

    I have a problem with "&" character. When I try to send a mail from web application and I include '&' to the post the whole message shows empty when send it through. So what can I do to prevent this?

    Thanks in advance!
    Hi,

    Can you supply the code that is doing this? If you are using HTML email you could try replacing the & with & which is the code for that character.

    If you could post the code it may help people help you more easily.

    Cheers
    nathj

    Comment

    • zamuel
      New Member
      • Sep 2006
      • 42

      #3
      Thanks for the quick reply. Yes I will post the code. It basically checks the users registered 'location' and according to that choose e-mail address to be send the post.

      [code=PHP]

      $city = $_GET['city'];

      $user = new user();
      $address = $user->SQL->query("selec t email FROM tblRegios WHERE plaats='".$city ."'");

      if (sizeof($result = $user->SQL->result())) {
      foreach($result as $key => $val)
      {
      if(isset($val['email']))
      $email = $val['email'];
      }
      }

      $to = $email;
      $subject = 'Mail';

      $headers = 'From';
      $vars = XML_unserialize ($_POST['xml']);

      mail($to, $subject, $vars['root']['opmerking'], $headers);
      [/code]

      So, what I want to do is replace '&' with something before sending and then decode it to be '&' again when it is being read.

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Thanks for posting the code. Where exactly is the & character that's causing the problems?

        What purpose is the & serving? Is it simply part of the message? If so could it simply be changed to 'and' using str_replace?

        Cheers
        nathj

        Comment

        • zamuel
          New Member
          • Sep 2006
          • 42

          #5
          Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Originally posted by zamuel
            Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
            Hi,

            In that case try replacing & with & in the message and with HTML email this should then be translated as & when the email client reads it.

            I'm not sure why the presence of the & character causes the problem you've got. I would be interested in finding that out.

            Cheers
            nathj

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by nathj
              Hi,

              In that case try replacing & with & in the message and with HTML email this should then be translated as & when the email client reads it.

              I'm not sure why the presence of the & character causes the problem you've got. I would be interested in finding that out.

              Cheers
              nathj
              To do so have a look at str_replace and functions like str_replace.

              Comment

              • zamuel
                New Member
                • Sep 2006
                • 42

                #8
                Yes, I got the point with that replace function. But for now it seems that it does not work. I does not even work when I try to replace '&' with 'and' as for test. And the fact remains that message text is deleted when '&' mark is posted. This is a very strange problem.

                Comment

                • nathj
                  Recognized Expert Contributor
                  • May 2007
                  • 937

                  #9
                  Hi,

                  Could you maybe post the message text? There maybe something in there that is causing the issue.

                  Cheers
                  nathj

                  Comment

                  • zamuel
                    New Member
                    • Sep 2006
                    • 42

                    #10
                    Well, the message text can be what ever user chooses to type in, but the text area itself is assigned through line

                    [code=php]
                    $page->assign("textOp merking",$form->textarea("opme rking","",40,15 ));
                    [/code]

                    and the text are itself is defined in different file like:

                    [code=php]
                    function textarea($name, $value="",$cols =40,$rows=4,$js = false,$class = false) {
                    $id = $name;
                    $array = array("name","t ype","cols","ro ws","class","id ");
                    foreach($array as $a) $return .= ($$a!==false) ? " $a=\"".$$a."\"" : "";
                    return $this->display($name, "textarea","<te xtarea ".$return." $js>".((isset($ this->values[$name])) ? $this->values[$name] : $value)."</textarea>");
                    }
                    [/code]

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, Zamuel.

                      What's an example of a message that is causing a problem? What happens when you try to run a str_replace() on it?

                      Comment

                      • zamuel
                        New Member
                        • Sep 2006
                        • 42

                        #12
                        Well, as an example any of these strings will cause problems:

                        This is test & example mail.
                        This mail includes '&' sign.
                        !@#$%^&*()_+

                        With other words, when '&' is present it causes the error. I tried to replace the '&' with some example text and it does not work although for example replacing 't' with 'R' works perfectly and the message gets through with the replacement.

                        Comment

                        • nathj
                          Recognized Expert Contributor
                          • May 2007
                          • 937

                          #13
                          Originally posted by zamuel
                          Well, as an example any of these strings will cause problems:

                          This is test & example mail.
                          This mail includes '&' sign.
                          !@#$%^&*()_+

                          With other words, when '&' is present it causes the error. I tried to replace the '&' with some example text and it does not work although for example replacing 't' with 'R' works perfectly and the message gets through with the replacement.
                          I have run a simple test using the string you supplied and the str_replace function.

                          Code:
                          [PHP]
                          $lcVar = "This is test & example mail.
                          This mail includes '&' sign.
                          !@#$%^&*()_+" ;
                          echo $lcVar . '<br /><br />' ;
                          $lcVar2 = str_replace('&' , '_and_', $lcVar) ;
                          echo $lcVar2 . '<br /><br />' ;
                          [/PHP]
                          Output:
                          Code:
                          This is test & example mail. This mail includes '&' sign. !@#$%^&*()_+
                          
                          This is test _and_ example mail. This mail includes '_and_' sign. !@#$%^_and_*()_+
                          The str_replace there works fine.

                          Cheers
                          nathj

                          Comment

                          • zamuel
                            New Member
                            • Sep 2006
                            • 42

                            #14
                            XML_unserialize () is basically taking the XML string and making it to be object, which in this case can be used as $vars['root']['opmerking'] for posting the value of 'opmerking' as the text field for e-mail message.

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              You can echo the value, and the '&' character is properly escaped, but when you send the email, there is no message body; is that correct?

                              Comment

                              Working...