# Test

**URL:** https://forum.kirupa.com/t/test/293178
**Category:** Uncategorized
**Created:** [July 24, 2009, 9:24am UTC](https://forum.kirupa.com/t/test/293178 "2009-07-24T09:24:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![subhash\_purohit](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@subhash\_purohit](https://forum.kirupa.com/u/subhash_purohit)
#### Post date: [July 24, 2009, 9:24am UTC](https://forum.kirupa.com/t/test/293178/1 "2009-07-24T09:24:51Z")

</div>

A paragraph can be truncated in php by following procedures:

Call a function -\> truncate\_string($string, length);

```php
function truncate_string($data, $max)
    {
//String truncated by '.'
      $val=explode('.',$data);
      if(count($val)>1)
      {
      $max=2;
      $re = "^s*[^.?!]+[.?!]+s*";
        $out = "";
        for($i = 0; $i < $max; $i++) {
            if(ereg($re, $data, $match)) {
                //if a sentence is found, take it out of $data and add it to $out
                $out .= $match[0];
                $data = ereg_replace($re, "", $data);
            }
            else {
                $i = $max;
            }
        }
        
        return $out;
    
        }else
        {
  //truncate by words, call another function 
         $word=truncate_words($data);
         return $word;
        }
        
    }// function for truncate by words     
    function truncate_words($data)
    {
      $str=preg_replace('/s+?(S+)?$/', '', substr($data, 0,200));
      return $str;
    }

```

_have a nice teaser of long string body_.
