remove newlines from string and minimize whitespace (multiple whitespace characters replaced by one space)
useful for cleaning up text retrieved by HtmlPageCrawler::text() (nodeValue of a DOMNode)

@param string $string
@return string
186870
public static function trimNewlines($string)
    {
        $string = str_replace("\n", ' ', $string);
        $string = str_replace("\r", ' ', $string);
        $string = preg_replace('/\s+/', ' ', $string);
        return trim($string);
    }
384291
private function cleanString($string, $replaceWith)
    {
        $words = $this->getDirtyWordsFromString($string);

        if ($words) {

            foreach ($words as $word) {

                if (!strlen($word)) {

                    continue;
                }

                if ($replaceWith === '*') {

                    $fc = $word[0];
                    $lc = $word[strlen($word) - 1];
                    $len = strlen($word);

                    $newWord = $len > 3 ? $fc . str_repeat('*', $len - 2) . $lc : $fc . '**';

                } else {

                    $newWord = $replaceWith;
                }

                $string = preg_replace("/$word/", $newWord, $string);
            }
        }

        return $string;
    }
279968
protected function cleanExport($string)
    {
        $quote = '"';
        $slash = '\\';
        $newline = "\n";

        $replaces = array(
            "$slash" => "$slash$slash",
            "$quote" => "$slash$quote",
            "\t"     => '\t',
        );

        $string = str_replace(array_keys($replaces), array_values($replaces), $string);

        $po = $quote . implode("${slash}n$quote$newline$quote", explode($newline, $string)) . $quote;

        // remove empty strings
        return str_replace("$newline$quote$quote", '', $po);
    }
543941
private static function removeParenthesis(string $string): string
    {
        if (substr($string, 0, 1) === '[' && substr($string, -1) === ']') {
            return substr($string, 1, strlen($string) - 2);
        }

        return $string;
    }
1355771
private static function parseline($string){
        $string = str_replace(chr(10), "\\n", $string);
        $string = str_replace(chr(13), "\\n", $string);
        $string = str_replace("\f", "\\f", $string);
        return $string;
    }
842607
protected function strlen($string)
    {
        // don't count color codes
        $string = preg_replace("/\33\\[\\d+(;\\d+)?m/", '', $string);

        if (function_exists('mb_strlen')) {
            return mb_strlen($string, 'utf-8');
        }

        return strlen($string);
    }
589175
public function convertNewlines($string) {
        $string = str_replace("\r\n", "\n", $string);
        $string = str_replace("\r", "\n", $string);

        return $this->cleanNewlines($string);
    }
794010
public static function escape($string) {
    $escaped_chars = str_split(self::ESCAPED_CHARACTERS);
    $escaped_chars_replace = array_map(function($char) {
      return '\\' . $char;
    }, $escaped_chars);
    $string = str_replace($escaped_chars, $escaped_chars_replace, $string);
    // Ignored whitespace.
    return preg_replace('/\s+/u', ' ', $string);
  }
1327895
protected function expandCSSEscape($string)
    {
        // flexibly parse it
        $ret = '';
        for ($i = 0, $c = strlen($string); $i < $c; $i++) {
            if ($string[$i] === '\\') {
                $i++;
                if ($i >= $c) {
                    $ret .= '\\';
                    break;
                }
                if (ctype_xdigit($string[$i])) {
                    $code = $string[$i];
                    for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) {
                        if (!ctype_xdigit($string[$i])) {
                            break;
                        }
                        $code .= $string[$i];
                    }
                    // We have to be extremely careful when adding
                    // new characters, to make sure we're not breaking
                    // the encoding.
                    $char = HTMLPurifier_Encoder::unichr(hexdec($code));
                    if (HTMLPurifier_Encoder::cleanUTF8($char) === '') {
                        continue;
                    }
                    $ret .= $char;
                    if ($i < $c && trim($string[$i]) !== '') {
                        $i--;
                    }
                    continue;
                }
                if ($string[$i] === "\n") {
                    continue;
                }
            }
            $ret .= $string[$i];
        }
        return $ret;
    }
299783
public static function toSingleWhitespace($string, $trim = true)
    {
        $string = preg_replace("/\r|\n|\t/", " ", $string);
        $string = preg_replace("/ {2,}/", " ", $string);

        if ($trim) {
            $string = self::trim($string);
        }

        return $string;
    }
208706
public static function whitespace_multiline($str='', int $newlines=1) {
		ref\sanitize::whitespace_multiline($str, $newlines);
		return $str;
	}
224944
private function clean($string) {
	   $string = str_replace(' ', '', $string);
	   $string = str_replace('-', '', $string);
	   return preg_replace('/[^A-Za-z0-9\-]/', '', $string);
	}
57164
public function normalize($string) {
		$string = $this->_normalize($string, ['clean' => true, 'case' => true]);
		return implode('-', str_split($string, $this->_partLength));
	}
988899
protected function tokenize($string)
    {
        $string = preg_replace('(\\r\\n|\\r)', "\n", $string);
        $tokens = array();
        $line = 1;

        while (strlen($string)) {
            foreach ($this->tokenSpecifications as $token => $regexp) {
                if (!preg_match($regexp, $string, $matches)) {
                    continue;
                }

                // Remove matched string
                $string = substr($string, strlen($matches[0]));
                $line += substr_count($matches[0], "\n");

                // Append token to list
                $tokens[] = array(
                    'type' => $token,
                    'data' => $this->filterValues($matches),
                );

                // Continue with outer loop
                continue 2;
            }

            throw new RuntimeException("Parse error in line $line: " . substr($string, 0, 100));
        }

        return $tokens;
    }
310105
public function clean($string)
    {
        $reg = '#'.preg_quote($this->browser->getHost()).'/ch\d+ \[([^\]]+)\]#';
        $string = preg_replace($reg, '$1', $string);
        $string = preg_replace('/\[[^\[\]]+\]/', '', $string); // remove BB tags

        return $string;
    }
569466
public function bBversHTML ($string = '')
    {
                $this->parserBB->setText($string);
                $this->parserBB->parse();
                return $this->parserBB->getParsed();
    }
487419
public function escape($string)
    {
        if(is_string($string)) {
            $string = htmlspecialchars($string, ENT_COMPAT | ENT_SUBSTITUTE, 'UTF-8', false);
        }

        return $string;
    }