用"..."缩短字符串身体 [英] Shorten string with a "..." body

查看:125
本文介绍了用"..."缩短字符串身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像iPhone的应用程序名称过长一样,名称也被缩短.我真的很喜欢这种缩短名称或字符串的方法,而不是在其后附加"..."子句的方法.抱歉,如果我感到困惑,我无法解释我要做什么.因此,我将举一个例子!

Just like the the iPhone's app names that run to long, the name gets shortened. I really like this method of shorting a name or a string rather then appending a "..." clause to it. Sorry if I am being confusing, I'm having trouble explaining what I am trying to do. So I'll show an example!

这就是我要在短字符串后附加"..."的功能(在PHP中)

This is what I have, to append "..." to a shortened string (In 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个字母并显示它们.我在想一些类似的事情:

I would like to split up the name or string and keep the first say 10 characters, then insert "..." in the middle and lastly take the ending 5 letters of the string and display them. I was thinking of something along the lines of:

<?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个字母,这是行不通的.任何指向写入方向的指针都很好,谢谢!

But realize that will not work in the regards that there are more then just 5 letters in the end. Any pointers into the write direction would be great, Thanks!

推荐答案

if (strlen($string) >= 20) {
    echo substr($string, 0, 10). " ... " . substr($string, -5);
}
else {
    echo $string;
}

这篇关于用"..."缩短字符串身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆