TheNeosrb TheNeosrb - 1 year ago 106
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

Answer Source

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 + "\\");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download