kkreft
7/1/2014 - 11:51 AM

PHP cut text 2 specified no' of words

function _shorten_string($oldstring, $wordsreturned)
	{
	  $retval = $string;
	  $string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $oldstring);
	  $string = str_replace("\n", " ", $string);
	  $array = explode(" ", $string);
	  if (count($array)<=$wordsreturned)
	  {
	    $retval = $string;
	  }
	  else
	  {
	    array_splice($array, $wordsreturned);
	    $retval = implode(" ", $array)." ...";
	  }
	  return $retval;
	}