1

I load prettify.js and format my code as such:

x = a;
y = b;

It comes out all proper and pretty, but... I need to squeeze this code into an SQL column so it needs to be in one line without any hidden line breaks. I tried

x = a;\ny = b;

That doesn't work because \n is escaped within the prettified block so it comes out as above. Is there any way to "code in" line breaks into the prettified code?

Perhaps I'm looking at the problem from the wrong angle? My ultimate goal is to put the prettify code block into MySQL table. I have PHP and JavaScript at my disposal.

Arthur Tarasov
  • 3,517
  • 9
  • 45
  • 57
  • What is the purpose of storing _code_ in database rather than storing values? – B001ᛦ Mar 21 '17 at 12:48
  • 3
    Isn't putting code all on one line the opposite of "prettifying"? – nnnnnn Mar 21 '17 at 12:49
  • If you are using a text data type in your database then the only processing that you will need to do is escape/unescape. –  Mar 21 '17 at 12:49
  • @bub I just want to pull it out and send it straight to the HTML file already formatted properly – Arthur Tarasov Mar 21 '17 at 12:49
  • you mean storing as plain text? No runable code? – B001ᛦ Mar 21 '17 at 12:51
  • @jeff Yes, it is stored as text. How can I escape/unescape it? – Arthur Tarasov Mar 21 '17 at 12:52
  • *"My ultimate goal is to put the prettify code block into MySQL table. I have PHP and JavaScript at my disposal."* - There's no code to support the php/mysql tags. – Funk Forty Niner Mar 21 '17 at 13:02
  • 1
    *"That doesn't work because /n"* - the syntax for linefeed/returns is `\n` and not `/n` last I knew, unless there's something I'm not aware of. or the escape character being \ and not / (in php anyway). Maybe using 2x escapes, such as // or \\ ---- Edit: to which you edited. – Funk Forty Niner Mar 21 '17 at 13:04
  • 1
    line breaks in php need to be inside double quotes `"\n"` in order to produce a line feed. You may also try `nl2br()` being a php function. I don't know what the source of your php is and if this is a php-related problem. JS does not have a line break character, so at best you'd probably need to do this in php and then handle the line breaks at the time of output. – Funk Forty Niner Mar 21 '17 at 13:13
  • 1
    You could probably implement `document.write("\n");` somehow though; best I can think of at this point. – Funk Forty Niner Mar 21 '17 at 13:14
  • can you just find replace "\\n" and replace it with an empty string, or if you still want to be able to view the code later just replace with "\\\\n" and then reverse the replacement when you read from SQL. – in need of help Mar 21 '17 at 14:02

1 Answers1

2

You can use the following syntax: (x = a) && (y = b); [node] ($x = $a) && ($y = $b); [php] Generally speaking you can take every statement, surround it by brackets and add the && between them

dWinder
  • 11,597
  • 3
  • 24
  • 39