-1

How am I able to turn my string into only 20 characters long?

<?php print htmlspecialchars_decode($artist, 0, 20, "..."); ?>

Currently the code above displays an error.

<b>Warning</b>:  htmlspecialchars_decode() expects at most 2 parameters, 4 given in <b>/some/domain/path/some-file.php</b> on line <b>45</b><br />

How can I get around this error but still use htmlspecialchars_decode() with 20 character long title?

Ritzy
  • 379
  • 1
  • 10
  • 3
    Possible duplicate of [Add ... if string is too long PHP](http://stackoverflow.com/questions/11434091/add-if-string-is-too-long-php) – mopo922 Jan 08 '16 at 23:16

2 Answers2

2

You need to apply separate functions for that, substr and strlen:

$short_artist = htmlspecialchars_decode($artist);
if (strlen($short_artist) > 20) {
    $short_artist = substr($short_artist, 0, 17) . "...";
}

If the three dots can make the total 23 characters long, then change the 17 to 20.

trincot
  • 317,000
  • 35
  • 244
  • 286
1

Try:

print substr(htmlspecialchars_decode($artist), 0, 20);

Also see in the docs: substr()

ArSeN
  • 5,133
  • 3
  • 19
  • 26