<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2012 - 2014 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flTextSplitWords Class
 *
 * A static class to split a string on a word boundary.
 *
 * @package fusionLib
 */
class flTextSplitWords {

	/**
	 * Split a string on a word boundary and return the 1st portion.
	 *
	 * Any HTML in the string is stripped.
	 *
	 * @param string $string The string to split.
	 * @param int $words The number of words to return from the start of the string.
	 * @param string $ending The ending to append to the string.
	 * @return string The leading $words from the string.
	 */
	static function split($string, $words = 30, $ending = ' &hellip;') {
		// Strip HTML from the string
		$t = flTextStrip::tags($string);
		if(!empty($t)) {
			$t = join(' ', array_slice($w = explode(' ', $t, $words + 1), 0, $words));
			if(count($w) > $words)
				$t .= $ending;
		}
		return $t;
	}
}