How to read file line using php in javascript variable without interrupt character?

I have a txt file on the server that contains 10 lines of text. Sometimes the text file is overwritten and I get new lines using "\ r \ n". My problem shows when I want to load strings in javascript variables. I do it this way, but it only works for numbers or for the last line of the file, because it s not using the breakline tag: var x = '<?php echo $file[0]; ?>'; Ive tried to warn (x), but it doesn’t work ... (only works if I read the Last line) Any ideal?

+3
source share
3 answers

Thanks everyone! I solved the problem with artlung and Richard JP Le Guen:

var x = <?php rtrim($file[0]); ?>
0

, , json_encode(),

var x = <?php echo json_encode($file[0], JSON_HEX_TAG); ?>;

, trim() / . - json_encode() , javascript (, ).

+3

trim():

var x = '<?php echo trim($file[0]); ?>';

file(), array_map() trim(), , :

$values = array_map("trim", file('input-file.txt'));

, , - .

EDIT: json_encode(), :

var x = <?php echo json_encode($file[0]); ?>;
0

Source: https://habr.com/ru/post/1743517/


All Articles