如何处理字符粘在一起的长字符串

时间:2016-07-22 08:17:45

标签: php html css string text

字符串类型1 :他最古老的古希腊语和拉丁语写作在单词之间几乎没有空格,可以用boustrophedon(交替方向)书写。随着时间的推移,文字方向

字符串类型2: asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd

Example when only with string type 1

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

如果我使用css文本溢出来控制文本:

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>

1 个答案:

答案 0 :(得分: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