/** * Strips session links, html entities and \r\n from the given text. * * @param string $text * @return string */ public static function stripCrap($text) { // strip session links, security tokens and access tokens $text = Regex::compile('(?<=\\?|&)([st]=[a-f0-9]{40}|at=\\d+-[a-f0-9]{40})')->replace($text, ''); // convert html entities (utf-8) $text = Regex::compile('&#(3[2-9]|[4-9][0-9]|\\d{3,5});')->replace($text, new Callback(function ($matches) { return StringUtil::getCharacter(intval($matches[1])); })); // unify new lines $text = StringUtil::unifyNewlines($text); // remove 4 byte utf-8 characters as MySQL < 5.5 does not support them // see http://stackoverflow.com/a/16902461/782822 $text = preg_replace('/[\\xF0-\\xF7].../s', '', $text); // remove control characters $text = preg_replace('~[\\x00-\\x08\\x0B-\\x1F\\x7F]~', '', $text); return $text; }