Function subwords, gets words by max string length

/**
 * Get whole words from string...
 *
 * @param string $str String of words
 * @param integer $int Maximum string length
 * @param string $strApend Apend string if string will be cutted
 */
function subwords($str, $int, $strAppend='...') {
	if (strlen($str) > $int) {
		$arr = str_word_count($str, 2);
		$blubb = false;
		foreach ($arr as $intPos => $strWord) {
			if ($blubb) return substr($str, 0, $intPos).$strAppend;
				if ($intPos > $int) $blubb = true;
		}
	}

	return $str;
}

POST A COMMENT

  • You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>