0

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 \n line break right there") ,
 ...
);


 <title><?php echo htmlspecialchars($CONFIG['subtitle']); ?></title>

Result

   My Sentence shall have a <br> line break right there
5
  • what is your question? Can you try to be more explicit? Commented Sep 11, 2015 at 11:49
  • @NaijaProgrammer updated my question Commented Sep 11, 2015 at 11:51
  • 2
    You do know what htmlspecialchars() does, don't you? If you have to use htmlspecialchars(), do it before the nl2br() Commented Sep 11, 2015 at 11:52
  • 1
    htmlspecialchars translates all < and > to &lt; and &gt;, rendering your html tags to not be interpreted by the browser anymore. If you want to have an actual <br> tag in there, avoid htmlspecialchars. Commented Sep 11, 2015 at 11:53
  • <title> tag doesn't have a line break or i am missing something? Commented Sep 11, 2015 at 12:04

4 Answers 4

1

As most answers have suggested, remove the htmlspecialchars section. Also, your \n should be replaced with \r\n for environments that don't recognize only the \n. The updated code should look like below:

<?php return array(
  ....
  'subtitle'    => nl2br("My Sentence shall have a \r\n line break right there") ,
 ...
);


 <title><?php echo $CONFIG['subtitle']; ?></title>

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Change your code with:

<?php return array(
  ....
  'subtitle'    => "My Sentence shall have a \n line break right there",
 ...
);


 <title><?php echo nl2br(htmlspecialchars($CONFIG['subtitle'])); ?></title>

Comments

1

The htmlspecialchars does not do

"\n" => "<br/>"

http://php.net/htmlspecialchars

The translations performed are:

'&' (ampersand) becomes '&amp;'
'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
"'" (single quote) becomes '&#039;' (or &apos;) only when ENT_QUOTES is set.
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;'

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

1 Comment

Or nl2br(htmlspecialchars($string));
0

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

2 Comments

He wants to replace \n with <br/>, your solution does not do that.
@mikeb the issue is not what that "he wants to replace \n with <br/>" (hes already doing that with nl2br), and therefore my solution does not do that. My reply provides a solution to his problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.