Display limited characters from text

Hi friends !! It is the function to display limited characters from a string without cutting the last word. If we use only substr the last word is not displayed properly. Using this custom function, we can solve this.

We just have to pass the string and the limit to the function. It will return the result string. Thanks !!

  function showString($string, $pos){
      $data = substr($string, $pos, 1);
      $len  = strlen($string);
      if ($data != " ") {
          while ($data != " ") {
              @$pos = $pos + 1;
              $data = substr($string, $pos, 1);
              if ($pos > $len) {
      $data = substr($string, 0, $pos);
      return $data;

Call function like this : -
echo showString('This is a little blog of php functions and source codes',30);
Display limited characters from text Display limited characters from text Reviewed by Altaf Hussain on February 09, 2011 Rating: 5

No comments:

Altaf Web. Powered by Blogger.