I have an array with strings. The strings shall have line breaks. My code produces the
as text not as tag. How can I manage to create a
tag in the HTML that creates a line break?
<?php return array(
....
'subtitle' => nl2br("My Sentence shall have a
line break right there") ,
...
);
<title><?php echo htmlspecialchars($CONFIG['subtitle']); ?></title>
Result
My Sentence shall have a <br> line break right there
As most answers have suggested, remove the htmlspecialchars
section. Also, your should be replaced with
for environments that don't recognize only the
. The updated code should look like below:
<?php return array(
....
'subtitle' => nl2br("My Sentence shall have a
line break right there") ,
...
);
<title><?php echo $CONFIG['subtitle']; ?></title>
Hope that helps.
The htmlspecialchars does not do
" " => "<br/>"
http://php.net/htmlspecialchars
The translations performed are:
'&' (ampersand) becomes '&' '"' (double quote) becomes '"' when ENT_NOQUOTES is not set. "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is set. '<' (less than) becomes '<' '>' (greater than) becomes '>'
Edit: As the comment below says, http://php.net/nl2br is the best solution.
You need to use a str replace or regex replace or something: http://php.net/manual/en/function.str-replace.php
If you skip the htmlspecialchars
you get the desired behaviour
echo $CONFIG['subtitle'];
It replaces your tags so they can be printed in html, and you dont want that
Change your code with:
<?php return array(
....
'subtitle' => "My Sentence shall have a
line break right there",
...
);
<title><?php echo nl2br(htmlspecialchars($CONFIG['subtitle'])); ?></title>