danielstjules / Stringy

A PHP string manipulation library with multibyte support
MIT License
2.46k stars 216 forks source link

Line break normalization #207

Open quantum3k opened 2 years ago

quantum3k commented 2 years ago

I suggest add feature for normalization line breaks. RegEx have modifier \R that matches any Unicode newline sequence. Equivalent to (?>\r\n|\n|\x0b|\f|\r|\x85). And the sources texts are sometimes very unpredictable with line breaks.

May be conside normalization method something like

    /**
     * Normalizes all line endings in this string by using a single unified newline sequence (which may be specified manually)
     *
     * @param string $text source text for normalize
     * @param string|null $newlineSequence the target newline sequence to use (optional)
     * @return string normalized text
     */
    public function normalizeLineEndings($text, $newlineSequence = null) {
        if ($newlineSequence === null) {
            $newlineSequence = "\n";
        }

        return \preg_replace('/\R/u', $newlineSequence, $text);
    }

(С) Borrowed this handy solution from the library https://github.com/delight-im/PHP-Str