doujing5435 2011-12-11 21:17
浏览 33
已采纳

如何使用wordwrap或其他方法来破坏文本以适应流体宽度div

I have a div which has a fluid width, thus I cannot predict its size in PHP. If someone enters a long string which isn't delimited by spaces anywhere, the browser will stretch the page, and that gets quite annoying rather quickly. I have tried to use the wordwrap function for this, but I can never find the proper width and I am having issues with output.

Here's what I am trying, based on a comment left on the documentation page for wordwrap:

<?php
//Using &#8203; here allows the browser to break the line of text, without the visual distraction. 
$line = wordwrap($line, 10, "&#8203;", true);
?>

The comment goes on to explain how it's a Zero Width Space character, which will tell the browser it is free to break but doesn't show up.

I'm sure you can already see the two problems which have arisen. It will replace normal spaces with that zero width space as well, so I wind up with things like: This is a​test.​Hello,​Stackoverf​low! This​ought to​trigger a​couple of​breaks.

I also coded a small bbcode parser for this, and it also breaks my html output since it cuts the tags.

On the plus side, incredibly long words are broken as expected!

I've tried setting various overflow properties on the div in CSS, but none of them function as expected.

  • 写回答

2条回答 默认 最新

  • dtyyrt4545 2011-12-11 21:51
    关注

    There is a CSS property of:

    word-wrap: break-word;
    

    Otherwise if you want to keep this PHP, Idealy you only want to apply a break on words with character counts greater than X, This will split the string on the space and apply a check to each word. You may run into problems with links, but you could easily check for starting url characters or apply a regex.

    $text = 'this text is fine but waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay long stuff gets broken waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay  up into bits';
    
    if($array = explode(' ',$text) and is_array($array))
    {
      foreach($array as $key => $value)
      {
        if(strlen($value) > 10)
          $array[$key] =  wordwrap($value, 10, "&shy;", true);
      }
      $text = implode(' ',$array);
    }
    
    echo $text;
    

    In this example words with a length greater than 10 are then word wrapped with the &shy; character, which is pretty useful as it produces a soft hyphen on breaks. Replace it with your breaking character if you would perfer no additional hyphens. You can also try the word wrap with a lower number than the max length before breaking, IE strlen > 20, word wrap at 10

    Sample: http://i.imgur.com/fKINR.png

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘