0

I have a question I have been pondering for a while.

What is the best way of keeping line breaks to a MySQL db and handle the escapes so that the query doesn't fail.

I came up with a function to do this but I am not sure this is the right way to handle the string.

function nl2br2 ($string){
    $sqlescapes = array("'", ";");
    $string = str_replace($sqlescapes, "", $string); 
    return str_replace( "\n", '<br />', $string ); 
}

This function will replace PHP line breaks and replace them with <br/> so html can recognise them if I use mysqli_real_escape_string it removes the line break and this is the predicament.

Are there any other functions I could utilise, as the above removes grammar from the string?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
D Jones
  • 76
  • 10

0 Answers0