JustinK101 Posted June 26, 2007 Share Posted June 26, 2007 How do I convert acutal line breaks into \n. I have a function which displays a javascript alert if a mysql query fails. The alert displays the full mysql query, that problem is that some of my queries are structured with line breaks such as: SELECT first_name last_name FROM customers WHERE company_name = 'ABC' These line breaks, cause the javascript alert code to fail, since javascript cannot deal with acutal new lines. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
marcus Posted June 26, 2007 Share Posted June 26, 2007 Are you talking about: \n to <br> or <br> to \n ? Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 <?php $s = str_replace("\n","\\n", $s); ?> Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 26, 2007 Author Share Posted June 26, 2007 Thorpe, There inst any \n displayed, they are acutal new line characters, the hidden guys. For example I pass in a string like: $sql = "SELECT first_name, last_name FROM customers WHERE first_name = 'bob'"; See what I mean, there are newlines in that string, and I echo that string into a javascript alert() if there is an error. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Did you try my code? This... #!/usr/bin/php <?php $s = "this is foo"; $s = str_replace("\n","\\n",$s); echo $s; ?> Produces... this\nis\nfoo Is this what you want? Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 26, 2007 Author Share Posted June 26, 2007 Thorpe, Ok so it almost worked, I changed the code to: str_replace("\n", '', $sql) Below (attached) is a screen shot of the alert window. You'll notice there is still some weird stuff going on, I think the whitespace might be tabs perhaps? [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 26, 2007 Author Share Posted June 26, 2007 Whoo, Whoo. This seemed to work: str_replace("\t", '', (str_replace("\n", '', $sql_query))) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.