【问题标题】:How to handle long string where characters stick together如何处理字符粘在一起的长字符串
【发布时间】:2016-07-22 08:17:44
【问题描述】:

String type 1:最古老的古典希腊文和拉丁文文字之间几乎没有空格,可以写成 boustrophedon(交替方向)。随着时间的推移,文字方向

字符串类型 2: asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd

Example when only with string type 1

当我将字符串类型 2 添加到表中时,字符串将从表中延伸出来

如果我使用 css text-overflow 来控制文本:

Here is with text-overflow css

我的问题是如何让字符串类型 1 显示为第一个图像,字符串类型 2 显示在 text-overflow css 中

提前感谢您的帮助!

table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Rules</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; ?>
                    @foreach ($companyrule as $companyrules)
                    <tr class="bu">
                        <th><?php echo $i++; ?></th>
                        <th> {{ $companyrules->rules}}</div></th>
                    </tr>
                        @endforeach
                    </tbody>
                </table>

【问题讨论】:

  • 添加你的css文件
  • 你可以用这个table td, table th { word-wrap: break-word; }

标签: php html css string text


【解决方案1】:

一种方法是检查字符串是否超过最大长度并且不包含空格。如果是这样,则回显字符串的substr() 并附加一些省略号,否则回显整个字符串。类似(未经测试):

$max_length = 20; // the maximum length of a string with no spaces

@foreach ($companyrule as $companyrules)
    <tr class="bu">
        <th><?php echo $i++; ?></th>

        <?php if ( strlen($companyrules->rules) > $max_length && (strpos($companyrules->rules, ' ') === false) {
             <th> {{ substr($companyrules->rules,0,$max_length )}}...</div></th>
        <?php } else { ?>
             <th> {{ $companyrules->rules}}</div></th>
        <?php } ?>

   </tr>
@endforeach

对于没有空格的长字符串,这会回显:

asdasdasdasdasdasda...

【讨论】:

  • 知道了!!感谢您的解决方案。
  • 很高兴。如果您不正确,请告诉我
猜你喜欢
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 2012-09-28
  • 2017-06-23
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多