douben1891
2015-09-11 11:48
浏览 68
已采纳

PHP - String中的换行符不起作用

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

图片转代码服务由CSDN问答提供 功能建议

我有一个包含字符串的数组。 字符串应有换行符。 我的代码生成的文本不是标签。 如何设置在HTML中创建一个创建换行符的
标签?

 &lt;?php return array(
 .... 
'
' 副标题'=&gt; nl2br(“我的句子在那里有一个
换行符”),
 ... 
); 
 
 
&lt; title&gt;&lt;?php echo htmlspecialchars($ CONFIG)  ['字幕']);  ?&gt;&lt; / title&gt; 
   
 
 

结果

 我的句子应该有&lt; br&gt; 换行
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • donoworuq450547191 2015-09-11 12:06
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doulou0882 2015-09-11 11:53

    The htmlspecialchars does not do

    " " => "<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

    评论
    解决 无用
    打赏 举报
  • duanmeng1858 2015-09-11 11:54

    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

    评论
    解决 无用
    打赏 举报
  • dsfbnhc4373 2015-09-11 11:57

    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>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题