Yes I know it should be easy - but it doesn't work:

I have this code:

$msg = "Hello,
              Nice to be here!";

          $msg=str_replace("\n","",$msg);

          echo "<script language=\"JavaScript\" type=\"text/javascript\">
          alert ($msg);
          </script>

Still I have the line breaks so the javascript can't run.

Any ideas?
Assaf

    You need to quote the value in the JS alert() call:

    <?php
    $msg = "Hello,
    Nice to be here!";
    
    $msg=str_replace("\n"," ",$msg);
    
    echo "<script language=\"JavaScript\" type=\"text/javascript\">
    alert ('$msg');
    </script>";
    ?>
    

      Thanks, but it is the same problem.
      When I view the source it looks like:
      alert ('Hello,
      Nice to be here!')

      which is no good 🙁

        Maybe you're using a WinDOS editor (such as Notepad) that uses "\r\n" for newlines?

          Simply remove \n AND \r...

          $newString = str_replace(array("\n", "\r"), '', $oldString);
          

            trim() won't remove line breaks inside a string, only at the left and right ends, not "in the middle" !

              Whoops........I just realised reading through the thread again. Was more concerned about whether a tree was about to come through the window.

                VERY windy here too.

                Anyway, if you want the newlines to display in the alert message, you need to replace them with \n.

                So, the above replace code modified would look like:

                $newString = str_replace(array("\n", "\r"), array('\n','\r'), $oldString); 

                I know that looks bizarre, but it works.

                  Write a Reply...