i found this Limit Characters From Your Text

but i cant use it when my text became less than 2 words
please any one can fi it

nav33n commented: Stop promoting that website. -2

Recommended Answers

All 3 Replies

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;
    }
  }
}

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.