【问题标题】:&nbsp in worwrap not working&nbsp 在 worwrap 中不起作用
【发布时间】:2023-03-28 03:18:02
【问题描述】:

我正在尝试修复冗长字符串的对齐问题,这是我的代码

  <html>
    <head></head>
    <body>
    <?php $lengthyString = "This is the lengthy string ,but i miss the spacing after second line. The length and contents may vary"; ?>
    <p style="width:25%">&nbsp;Line1<br>
    &nbsp;Line2<br>
    &nbsp;<?php echo nl2br($lengthyString); ?>z<br>
    </p>

    </body>
    </html>

但我错过了$lengthyString中的间距(&nbsp)调整。我想显示长度字符串的新行应该与一个空格一致。然后我尝试了wordwrap()

<?php echo wordwrap($lengthyString,25,"&nbsp;<br>\n");?>

但这不起作用,有什么解决方案可以将&amp;nbsp 包含在&nbsp 中wordwrap

预期输出:

 Line1
 Line2
 This is the lengthy 
 string ,but i miss the 
 spacing after second 
 line. The length and 
 contents may vary

但我是这样的

 Line1
 Line2
  This is the lengthy 
string ,but i miss the 
spacing after second 
line. The length and 
contents may vary

【问题讨论】:

  • 解释“它不起作用”...您是在浏览器中查看源代码还是仅查看渲染?如果我理解wordwrap 更有意义。 nl2br 没有,因为我在该字符串中没有看到“新行”。
  • 我错过了下一行的间距,每一行开始都应该是统一的,这就是我正在尝试的事情
  • 没有换行符可以转换成
  • 你的输出是什么,你真正想要的输出是什么。对第 1 行、第 2 行等使用 list 和 aling
  • 完全摆脱&amp;nbsp;s,使用CSS进行格式化。

标签: php html css php-7


【解决方案1】:

根据上面的评论:

<html>
    <head></head>
    <body>
    <?php $lengthyString = "This is the lengthy string ,but i miss the spacing after second line. The length and contents may vary"; ?>
    <p style="width:25%;">
        Line1<br>
        Line2<br>
        <?php echo $lengthyString; ?><br>
    </p>

    </body>
    </html>

对于&lt;p&gt;&lt;/p&gt; 标签内的间距,请使用 css。 &amp;nbsp 也是一个字符,所以你只是在前面多放了一个字符。

更新

<html>
    <head>
    <style>
    p{
        white-space: nowrap;
    }
    </style>
    </head>
    <body>
    <?php $lengthyString = "This is the lengthy string ,but i miss the spacing after second line. The length and contents may vary"; ?>
    <p style="width:25%;">
        &nbsp;Line1<br>
        &nbsp;Line2<br>
        &nbsp;<?php echo $lengthyString; ?><br>
    </p>

    </body>
    </html>

【讨论】:

  • 谢谢,不过这个小部分还可以,但是在实际代码中我不能去掉&nbsps,会影响其他部分
  • 尝试使用 css 的 white-space 属性解决问题。我已经更新了帖子。查看更新
  • 我想多行显示冗长的内容来调整每一行的宽度。
猜你喜欢
  • 2013-06-10
  • 2014-01-26
  • 2011-02-27
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2014-09-02
相关资源
最近更新 更多