mysql - Replacing \r\n with PHP -


I have a post form that includes my text in a MySQL database.

I use

  $ post_text = mysql_real_escape_string (htmlspecialchars ($ _ POST ['text'])); Want to automatically change   

and \ r \ n

I tried

  $ Text = str_replace ('\\ r \\ n', '', $ text); $ Text = str_replace ('\ r \ n', '', $ text); $ Text = str_replace ('\\ R \\ N', '', $ text); $ Text = str_replace ('\ R \ N', '', $ text); $ Text = str_replace ('/ \ r \\ n', '', $ text); $ Text = str_replace ('/ r / n', '', $ text); $ Text = str_replace ('/ \ r \\ N', '', $ text); $ Text = str_replace ('/ R / N', '', $ text);   

but \ r \ n is always included in my database entries.

How can I fix this?

The main problem with all the changes you make is that both \ n and \ r are escape characters that are saved only when you use them in double-quoted strings

in PHP '\ r \ N ' and "\ r \ n" . First, note double-quotation, and double quotation marks in the second.

Such: '\ r \ n' The four letters containing a slash will result in a string, one 'r', another slash and 'n', while " \ R \ n " will contain two letters, which are the new line and carriage return characters.

The direct answer to your question is that you need to use one of the examples given in the question, but with double quotation marks instead of single quotes:

 < Code> $ text = str_replace ("\ r \ N", '', $ text);   

It is worth pointing out that it will remove all new lines from the input, and nothing changes them. If there is more than one new line in these inputs, then all those will be deleted without knowing more about your application, I do not know that this is what you want, but here are some ideas: < Ul>

  • If you only use end of the empty rows (and other white spaces) input string, you can use the trim () function instead Are there.

  • If you want to retain the formatting of the output for HTML, then the nl2br () function will help you if you do not have HTML You do not have to remove them, because the browser will not provide line breaks from characters \ n characters.

  • If you change new lines with some examples, according to your example, the last word in the first line will now run directly in the first word of the second row, and so on. You may want to replace them with a space character instead of a place.

  • It is possible that users can only submit input with \ n matching \ r \ / code>, or vice versa ( This may be due to their OS or browser, or a deliberate hack, or many other reasons). If you want to change the all instances of these two letters, one more reliable way to do this is to replace them individually, rather than depend on them, rather than each other should be there. str_replace () Allows you to do this by specifying them in an array. You can also use strtr () or preg_replace () to achieve the same goal.

    Hope that helps

  • Comments

    Popular posts from this blog

    qt - switch/case statement in C++ with a QString type -

    python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

    Python's equivalent for Ruby's define_method? -