Shorten the string with the body "..."

Like iPhone app names that last longer, the name is abbreviated. I really like this method of locking a name or string, and then adding the sentence "..." to it. Sorry if I get confused, I am having trouble explaining what I'm trying to do. Therefore, I will set an example!

This is what I have, add "..." to the 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
?>

I would like to split the name or string and save the first 10 characters, then insert the “...” in the middle and finally take the end of the 5 letters of the string and display them. I was thinking of something like:

<?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);
?>

But understand that this will not work in the sense that in the end there are only 5 letters left. Any directional pointers would be great, thanks!

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

substr() - , . 5.

, substr($string, -5).

+2

Source: https://habr.com/ru/post/1729658/


All Articles