2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by click here
0

This post is just to see the function with formatting, I will try to help you shortly.

function text_limit($str,$limit=10)
{
  if(stripos($str," "))
  {
    $ex_str=explode(" ",$str);
    if(count($ex_str)>$limit)
    {
      for($i=0;$i<$limit;$i++)
      {
        $str_s.=$ex_str[$i]." ";
      }
      return $str_s;
    }
    else
    {
      return $str;
    }
  }
}
0

So if you have less than two words, there is no space. You need to add an "else" for your first if-statement, like so:

function text_limit($str,$limit=10)
{
  if(stripos($str," ");
  {
      $ex_str=explode(" ",$str);
      if(count($ex_str)>$limit)
      {
        for($i=0;$i<$limit;$i++)
        {
           $str_s.=$ex_str[$i]." ";
        }
        return $str_s;
      }
      else
      {
         return $str;
      }
   }
   else
   {
        return $str;
   }
}

Also, there are probably more efficient ways of achieving this than exploding and rebuilding the original string. But this quick fix should work for you.

0

So if you have less than two words, there is no space. You need to add an "else" for your first if-statement, like so:

function text_limit($str,$limit=10)
{
  if(stripos($str," ");
  {
      $ex_str=explode(" ",$str);
      if(count($ex_str)>$limit)
      {
        for($i=0;$i<$limit;$i++)
        {
           $str_s.=$ex_str[$i]." ";
        }
        return $str_s;
      }
      else
      {
         return $str;
      }
   }
   else
   {
        return $str;
   }
}

Also, there are probably more efficient ways of achieving this than exploding and rebuilding the original string. But this quick fix should work for you.

Oh thanks man iam sure it will work good

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.