就像 iPhone 的应用程序名称过长一样,名称会缩短。我真的很喜欢这种缩短名称或字符串而不是向其附加“...”子句的方法。对不起,如果我感到困惑,我无法解释我想做什么。所以我会举个例子!
这就是我所拥有的,将“...”附加到缩短的字符串(在 PHP 中)
<?php
$string = "This is a test script";
if (strlen($string) >= 14)
echo(substr($string), 0, 13). "..."); // This is a test...
else
echo($string); // This is a test script
?>
我想拆分名称或字符串并保留前 10 个字符,然后在中间插入“...”,最后取字符串的结尾 5 个字母并显示它们。我在想一些类似的事情:
<?php
$string = "This is a test script";
if (strlen($string) >= 20)
echo(substr($string, 0, 10). "..." .substr($string, 15, 20)); //This is a ...script
else
echo($string);
?>
但是请注意,考虑到最后只有 5 个字母,这将行不通。任何指向写入方向的指针都会很棒,谢谢!
最佳答案
if (strlen($string) >= 20) {
echo substr($string, 0, 10). " ... " . substr($string, -5);
}
else {
echo $string;
}
关于php - 用 "..."主体缩短字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2122042/