0

I use the following code for hyperlinks:

echo "<a href=\"http://".$row['website']."\" target=\"_blank\">".substr($row['website'],0,50).'…'."</a>";

This will short the displayed URL if to long and add a trailing . But I want only added if URL longer than declared value, in this case 50 characters.

What's the simples way to do that?

  • 2
    Possible duplicate of [Add ... if string is too long PHP](http://stackoverflow.com/questions/11434091/add-if-string-is-too-long-php) – The Codesee Feb 05 '17 at 10:45

1 Answers1

1

You can use mb_strimwidth:

echo "<a href=\"http://".$row['website']."\" target=\"_blank\">".mb_strimwidth($row['website'], 0, 50, '…')."</a>";

This will ensure the maximum length of the string is 50 characters and add '…' if it exceeds that.

The Codesee
  • 3,714
  • 5
  • 38
  • 78