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. Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/ 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 ? Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282533 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 <?php $s = str_replace("\n","\\n", $s); ?> Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282537 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. Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282576 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? Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282590 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] Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282784 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))) Link to comment https://forums.phpfreaks.com/topic/57181-convert-new-line-characters-to-n/#findComment-282791 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.