TheNeosrb TheNeosrb - 5 months ago 29
HTML Question

dynamically add backslash to end of string line

I have code that gets some text from database and inserts it inside

javascript var
. This is the code:

var = <?php echo $var ?>

When the page is loaded,
looks like this:

var = "This is first line
This is second
This is third..."

Is there a way to dynamically add a backslash after each line?


I am using
to echo the text and I want to manipulate it inside


Just split the variable into new lines with whatever delimiter you like:

$var = "This is line 1|##|This is line 2|##|This is line 3";
<script type='text/javascript'>
var lines = "<?=$var?>";
var res = lines.split("|##|");

res.forEach(function(entry) {
    alert(entry + "\\");