I would like to replace space in string to line break.
Before
1 2 3 4
After preg_replace :
1
2
3
4
This is my code:
$m_cart = preg_replace('/\s/', '\n', $session->get('cart'));
return $this->render('FooSiteBundle:Site:bar.html.twig', array('cart' => $m_cart));
and this is my view with twig:
{{ cart }}
the result:
\n1\n2\n3\n4
but I would like to have
1
2
3
4
The newline escape sequence
\nisn’t interpreted by single-quote-delimited strings (as a point, /[no]/ escape sequences are)The simple answer is to just change
'\n'to"\n".I invite you to read more about PHP’s string delimiters and how they differ