Is there a way to not expand escape sequences?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanaa
    New Member
    • Nov 2007
    • 44

    Is there a way to not expand escape sequences?

    Hello there.

    [PHP]$str="This is \na ball";
    echo $str;[/PHP]

    Is there a way i can make the text to be as is, without expanding the escape sequences. I know that single quoted strings do not expand escape sequences. But I need to echo text that's been entered by the user in a html textarea as is. And thats why I dont want the escape sequences to be expanded.

    Thanks,
    Hanaa
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You could just remove them
    Code:
    echo str_replace('\n', ' ' $str);
    Or am I missing the problem?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      You want it to show the escape characters... Like \" instead of "?

      If so, try running it through the addslashes function before printing it.

      Comment

      • hanaa
        New Member
        • Nov 2007
        • 44

        #4
        (I am sorry if i was unclear.)
        That would remove the '\n'. I want the '\n' to be printed as is.
        It isnt just the escape sequence '\n'. Say, if the text that the user's entered contains '\n' or '\r' or '\w'(which is not an escape sequence), I want all the text to be echoed as is.
        There's this textarea, where the user is supposed to enter some text which could contain words like '\none' (which cud contain escape sequences that are not to be interpreted). but i when i try to display the entered text on another page, all the escape sequences are being interpreted. But I want the text to be echoed exactly how the user wrote it.
        Or do i have to use str_replace to replace all the escape sequences..? replace '\n' with '\\n' and likewise for all the other escape sequences?

        Hanaa

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          How are you printing the text. Could you show us the code?

          PHP does not threat the \ character inside strings as and escape character unless it is being used from within a PHP script. So user input should not be parsed into their respective special characters by PHP.

          Keep in mind tho, that if your user enters something like: "I am \not two lines", that exact text will be printed to the browser.
          The browser, however, may very well convert the \n into a new-line char.

          By using the addslashes function, you will in fact be converting that string into "I am \\not two lines", which your browser should parse into "\n" rather than a new-line char.

          Edit.
          Be careful tho. If the magic_quotes_gp c directive is enabled in your PHP installation, the addslashes function will automatically be invoked on all user input before you get it.
          So if that is the case, calling it again will add extra backslashes.

          Comment

          • hanaa
            New Member
            • Nov 2007
            • 44

            #6
            Thanks Atli and markusn00b.
            It just occured to me that i forgot to mention that this has to do with MySQL too. Just posting a text to display it on another page, is indeed echoing the exact text(which is what i want). But when i save it in a database, retrieve and echo it, the escape characters are being interpreted. Here's the code. This isnt the page that i made. In those that i made, i ve used the same page to perform all edit, delete, add functions, so its messy. So i wrote down this.

            testa.php
            [PHP]<?php
            $conn=mysql_con nect('localhost ', 'root', 'password') or die(mysql_error ());
            mysql_select_db ('testd') or die(mysql_error ());
            if(isset($_POST['submit']))
            {
            $query="INSERT INTO tablename(quest ion) VALUES('".$_POS T['question']."')";
            $request=mysql_ query($query) or die(mysql_error ());
            header("locatio n:testb.php");
            }
            mysql_close($co nn);
            ?>

            <html>
            <head>
            <title> Test </title>
            </head>
            <body>
            <form action='testa.p hp' method='post'>
            Enter text<br/>
            <textarea name='question' rows='5' cols='60'></textarea>
            <input type='submit' name='submit' value='Go!'>
            </form>
            </body>
            </html>[/PHP]

            testb.php
            [PHP]<html>

            <head>
            </head>
            <body>
            <?php
            $conn=mysql_con nect('localhost ', 'root', 'password') or die(mysql_error ());
            mysql_select_db ('testd');
            $query="SELECT * FROM tablename";
            $result=mysql_q uery($query);
            while($row=mysq l_fetch_assoc($ result))
            {
            echo $row['question'];
            echo"<br/>";
            }
            ?>
            </body>
            </html>
            [/PHP]

            Eg.
            I enter the followin text on the first page-
            This is a \n bat.
            And on the next page i see whats below
            This is a bat.

            Am i supposed to post this in the MySQL section?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Hanaa.

              Try running the string through nl2br() before you display it.

              E.g., [code=php]echo nl2br($row['question']);[/code]

              Comment

              • hanaa
                New Member
                • Nov 2007
                • 44

                #8
                No pbmods, I want none of the escape sequences to be interpreted. not just '\n', others such as '\t' etc.

                Comment

                • hanaa
                  New Member
                  • Nov 2007
                  • 44

                  #9
                  And, I tried using addslashes() too. The outputs the same even then. The escape characters are being expanded.

                  Comment

                  • hanaa
                    New Member
                    • Nov 2007
                    • 44

                    #10
                    Thankyou everyone
                    It works now. I had to turn magic_quotes_gp c on in php.ini. (PHP 5)

                    Hanaa

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by hanaa
                      Thankyou everyone
                      It works now. I had to turn magic_quotes_gp c on in php.ini. (PHP 5)

                      Hanaa
                      Great news, Hanaa!

                      See you around.

                      Comment

                      Working...