Author Topic: Show specific length of a strings in PHP  (Read 5138 times)

bbasujon

  • Administrator
  • VIP Member
  • *****
  • Posts: 1826
  • I want to show my performance at any where
    • View Profile
    • Higher Education
Show specific length of a strings in PHP
« on: January 22, 2012, 07:40:32 AM »
To show any specific length of a strings in PHP we can use the following functions.

function excerpt($str, $length=10, $trailing=’…’)
{
/*
** $str -String to truncate
** $length – length to truncate
** $trailing – the trailing character, default: “…”
*/
// take off chars for the trailing
$length-=mb_strlen($trailing);
if (mb_strlen($str)> $length)
{
// string exceeded length, truncate and add trailing dots
return mb_substr($str,0,$length).$trailing;
}
else
{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
Acquire the knowledge and share the knowledge so that knowing,learning then sharing - all are the collection