用"......"体缩短字符串

Dav*_*vid 5 php string

就像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
?>
Run Code Online (Sandbox Code Playgroud)

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);
?>
Run Code Online (Sandbox Code Playgroud)

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!

Ali*_*xel 15

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