user7128548 user7128548 - 4 years ago 94
HTML Question

Add ellipses only if longer

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
. But I want
only added if URL longer than, in this case, 50 characters.

What's the simples way to do that?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download