Skip to content

Instantly share code, notes, and snippets.

@andreipa
Created May 12, 2020 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreipa/782209b08daecfc6ee60916540a18cff to your computer and use it in GitHub Desktop.
Save andreipa/782209b08daecfc6ee60916540a18cff to your computer and use it in GitHub Desktop.
Remove html tags and truncate string adding a trim marker.
/**
* Remove html tags and truncate string
*
* @link https://www.php.net/manual/en/function.mb-strimwidth.php
* @author Andrei Andrade
*
* @param string $string The string being decoded.
* @param int $width The width of the desired trim.
* @param string $trimMarker A string that is added to the end of string when string is truncated.
*
* @return string
*/
function truncate($string, $width, $trimMarker = '...')
{
$string = trim(strip_tags($string));
$string = mb_strimwidth($string, 0, $width, $trimMarker, "UTF-8");
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment