<?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) 2008 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flTextStrip Class
 *
 * A collection of Static functions for stripping data from UTF-8 text blocks.
 *
 * @package fusionLib
 */
class flTextStrip {

	/**
	 * Strip tags from a block of text, decode entities, remove white space leaving just
	 * one space between each word.
	 *
	 * @param string $text The text to clean
	 * @return string The stripped text.
	 */
	static function tags($text) {

		// Strip blocks of hidden text
		$text = preg_replace('#<(head|script|map|area|menu|marquee|select|textarea|style|object|embed|applet)[^>]*.*?</\1>#siu', ' ', $text);

		// Add a space after each block tag to avoid word joins on strip_tags
		$text = preg_replace('#</(address|blockquote|center|del|div|h[1-9]|ins|isindex|p|pre|dir|dl|dt|dd|li|menu|ol|ul)>#iu', ' ', $text);
		$text = preg_replace('#</(table|th|td|tr|caption|form|button|fieldset|legend|input|label|frameset|frame|iframe)>#iu', ' ', $text);
		$text = preg_replace('#<br[^>]*>#', ' ', $text);

		// Strip the remaining tags and decode entities
		$text = html_entity_decode(strip_tags($text), ENT_QUOTES, 'utf-8');

		// Reduce control, formatting, surrogate chars and multiple white space down to a single space
		return trim(preg_replace('/[\p{Z}\p{Cc}\p{Cf}\p{Cs}]+/u', ' ', $text));
	}

	/**
	 * Strip punctuation from a block of text.
	 *
	 * @param string $text The text to strip the numbers from.
	 * @return string The stripped text.
	 */
	static function punctuation($text) {
		$text = preg_replace(array(
			'/[\p{Z}\p{Cc}\p{Cf}\p{Cs}\p{Pi}\p{Pf},]/u',	// Remove separators, control, formatting, surrogagtes, quotes, comma
			'/((?<= )|^)[:;\'_\*%@&?!\[\]\(\)]+/u', // Remove web chars before or after a space
			'/[\.:;\'\-_\*@&\/\\\\\?!#\[\]\(\)]+((?= )|$)/u',
			'/[\p{Ps}\p{Pe}](?<![\[\]\(\)])/u', // Remove brackets
			'/((?<= )|^)[\'"\*_<>]+/u', // Remove quote chars before or after a space
			'/[\'"\*_<>]+((?= )|$)/u',
			'/((?<= )|^)\p{Pd}+(?![\p{N}\p{Sc}])/u', // Remove dashes
			'/\p{Pd}+((?= )|$)/u',
			'/((?<= )|^)\p{Pc}+/u', // Remove connectors
			'/\p{Pc}+((?= )|$)/u',
			'/[\x{002e}\x{fe52}\x{ff0e}\x{002c}\x{fe50}\x{ff0c}\x{066b}\x{066c}]+((?= )|$)/u', // Remove number separators
			'/\p{Po}(?<![\.,:;\'\-_\*%@&\/\\\\\?!#\[\]\(\)\x{002e}\x{fe52}\x{ff0e}\x{002c}\x{fe50}\x{ff0c}\x{066b}\x{066c}\'"\*_<>\x{0023}\x{fe5f}\x{ff03}\x{066a}\x{0025}\x{fe6a}\x{ff05}\x{2030}\x{2031}\x{2032}\x{2033}\x{2034}\x{2057}])/u', // Remove other punctuation
			'/ +/'
			), ' ', $text);

		// Remove ) in a number e.g. 0)123
		$text = preg_replace('/((^| )\d+)[\(\)\[\]](\d)/', '\1\3', $text);

		return trim($text);
	}

	/**
	 * Strip symbols from a block of text.
	 *
	 * @param string $text The text to strip the symbols from.
	 * @return string The stripped text.
	 */
	static function symbols($text) {
		return trim(preg_replace(array(
			'/\p{Sk}/u', // Remove modifier symbols
			'/\p{Sm}(?<![\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2012}\x{208b}\x{207b}=~\x{2044}])/u', // Remove maths symbols
			'/((?<= )|^)[\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2012}\x{208b}\x{207b}]+((?![\p{N}\p{Sc}])|$)/u',
			'/((?<= )|^)=+/u',
			'/[\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2012}\x{208b}\x{207b}=~]+((?= )|$)/u',
			'/\p{So}(?<![\x{00b0}\x{2103}\x{2109}\x{23cd}\x{32cc}-\x{32ce}\x{3300}-\x{3357}\x{3371}-\x{33df}\x{33ff}\x{2e80}-\x{2ef3}\x{2f00}-\x{2fd5}\x{2ff0}-\x{2ffb}\x{3037}-\x{303f}\x{3190}-\x{319f}\x{31c0}-\x{31cf}\x{32d0}-\x{32fe}\x{3358}-\x{3370}\x{33e0}-\x{33fe}\x{a490}-\x{a4c6}])/u', // Remove other symbols
			'/\p{Co}/u', // Private use symbols
			'/ +/'
			), ' ', $text));
	}

	/**
	 * Strip numbers from a block of text.
	 *
	 * @param string $text The text to strip the numbers from.
	 * @return string The stripped text
	 */
	static function numbers($text) {
		$number = '[\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2212}\x{207b}\x{208b}\p{Pd}]?[\p{Sc}#]?[\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2212}\x{207b}\x{208b}\p{Pd}]?[\p{N}\x{002e}\x{fe52}\x{ff0e}\x{002c}\x{fe50}\x{ff0c}\x{066b}\x{066c}]+([eE][\+\x{fe62}\x{ff0b}\x{208a}\x{207a}\x{2212}\x{207b}\x{208b}\p{Pd}]?[\p{N}\x{002e}\x{fe52}\x{ff0e}\x{002c}\x{fe50}\x{ff0c}\x{066b}\x{066c}]+)?([\p{Sc}%\x{ff05}\x{fe64}\x{2030}\x{2031}\x{00b0}\x{21023}\x{2109}\x{23cd}\x{32cc}-\x{32ce}\x{3300}-\x{3357}\x{3371}-\x{33df}\x{33ff}\x{fe64}\x{ff05}\x{2030}\x{2031}]|([aApP][mM]))?';
		return trim(preg_replace(array(
			'/((?<=[^\p{L}\p{M}\p{N}\p{Pc}\p{Pd}\.,:;\'=+\-_\*%@&amp;\/\\\\?!#~\[\]\(\)])|^)' . $number . '([\/\x{2044}]' .  $number . ')?([\x{2212}\x{208b}\x{2027}\p{Pd}:\x{fe55}\x{ff1a}\x{2236}\x{002e}\x{fe52}\x{ff0e}]' . $number . '([\/\x{2044}]' .  $number . ')?)*((?=[^\p{L}\p{M}\p{N}\p{Pc}\p{Pd}\.,:;\'=+\-_\*%@&amp;\/\\\\?!#~\[\]\(\)])|$)/u', // Remove fractions
			'/ +/'
			), ' ', $text));
	}
}
