nl2br not clearing all new lines
So I have some strange issue with nl2br so I have the following code:
var infowindow = new google.maps.InfoWindow({
content: "<?= nl2br($store["Stores"]["address"]) ?>"
});
the output when viewing the browser source is as shown in this image:
As you can see the <br />
have been correctly introduced but for some reason the line still breaks which causes a javascript error.
The variables values is what has been posted from the text area input of the form
as you can see from the examples in the documentation, this seems to be intended behaviour. if you want to replace all newlines to get the string into one line, a simple regex should do it. this is how i would try (typed out of my head, this isn't tested):
<?= preg_replace('/(\r\n|\n|\r)/','<br/>',$store["Stores"]["address"]) ?>
That's the expected behaviour of nl2br. If you want to remove the newlines in the string you'll have to use preg_replace.
<?= preg_replace("/(\r\n|\n|\r)/", "<br />", $input)
nl2br does not remove newlines, it just inserts < br /> before them.
http://php.net/manual/en/function.nl2br.php
This is your answer
var infowindow = new google.maps.InfoWindow({
content: <>"<?= nl2br($store["Stores"]["address"]) ?>"</>.toString()
});
精彩评论