Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]

Expanding on someone else’s answer: <script> var myvar = <?php echo json_encode($myVarValue); ?>; </script> Using json_encode() requires: PHP 5.2.0 or greater $myVarValue encoded as UTF-8 (or US-ASCII, of course) Since UTF-8 supports full Unicode, it should be safe to convert on the fly. Note that because json_encode escapes forward slashes, even a string that contains … Read more

How to find out line-endings in a text file?

You can use the file utility to give you an indication of the type of line endings. Unix: $ file testfile1.txt testfile.txt: ASCII text “DOS”: $ file testfile2.txt testfile2.txt: ASCII text, with CRLF line terminators To convert from “DOS” to Unix: $ dos2unix testfile2.txt To convert from Unix to “DOS”: $ unix2dos testfile1.txt Converting an … Read more

What is the JavaScript string newline character?

I’ve just tested a few browsers using this silly bit of JavaScript: function log_newline(msg, test_value) { if (!test_value) { test_value = document.getElementById(‘test’).value; } console.log(msg + ‘: ‘ + (test_value.match(/\r/) ? ‘CR’ : ”) + ‘ ‘ + (test_value.match(/\n/) ? ‘LF’ : ”)); } log_newline(‘HTML source’); log_newline(‘JS string’, “foo\nbar”); log_newline(‘JS template literal’, `bar baz`); <textarea id=”test” … Read more

What are carriage return, linefeed, and form feed?

Carriage return means to return to the beginning of the current line without advancing downward. The name comes from a printer’s carriage, as monitors were rare when the name was coined. This is commonly escaped as “\r”, abbreviated CR, and has ASCII value 13 or 0xD. Linefeed means to advance downward to the next line; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)