gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:27 25 4
gpt4 key购买 nike

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

字符串类型 2:asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd

Example when only with string type 1

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

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

Here is with text-overflow css

我的问题是如何让字符串类型 1 显示为第一个图像,并在文本溢出 css 中显示字符串类型 2

提前感谢您的帮助!!

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>

最佳答案

一种方法是检查字符串是否超过最大长度并且不包含空格。如果是,则回显字符串的 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...

关于php - 如何处理字符粘在一起的长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38521513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com