I have a return string from a db.
The return string must be formatted in javascript.
<?php
$db = "this is a line
this is a new line";
?>
How would I convert the above to:
<?php $db = "this is a line \n this is a new line"; ?>
Javascript:
<script>
var jdb = <?php echo $db; ?>
</script>
The \n character matches newline characters.
Try this (updated 2014-11-08):
<?php
$db = "this is a line
this is a new line
";
?>
<script type="text/javascript">
var jdb = <?php echo json_encode($db) ?>;
</script>
$db = preg_replace("/\n/m", '\n', $db);
should do the trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With