Saturday, April 14, 2018

Best way to replace \r\n with
or clean mysql escape string php

When we use mysqli_real_escape_string() function to secure the input value to store in mysql there  \r\n  automatically inserted for using enter or line break.

The main problem you have with all the variations you've tried is that both \n and \r are escape characters that are only escaped when you use them in a double-quoted string.

In PHP, there is a big difference between '\r\n' and "\r\n". Note the single-quotes in the first, and double-quotes in the second.

So: '\r\n' will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "\r\n" will contain two characters, those being the new line and carriage return characters.

As there is fact about quoted so as usual function may not work. I wasted several hours on it.
str_replace(), preg_replace(), nl2br()
So with out worrying quote here is best solution
str_replace(array('\r', '\n', '\r\n', "\r", "\n", "\r\n"), "", $txt);

So here we escape both single and double quote. 



No comments:

Post a Comment

Total Pageviews