I am trying to simply replace some new lines and have tried three different ways, but I don’t get any change:
$description = preg_replace('/\r?\n|\r/', '<br/>', $description);
$description = str_replace(array("\r\n", "\r", "\n"), "<br/>", $description);
$description = nl2br($description);
These should all work, but I still get the newlines. They are double: "\r\r". That shouldn’t make any of these fail, right?
There is already the
nl2br()function that inserts<br>tags before new line characters:Example (codepad):
But if it is still not working make sure the text
$desciptionis double-quoted.That’s because single quotes do not ‘expand’ escape sequences such as
\ncomparing to double quoted strings. Quote from PHP documentation: