]*>.*?#is', '', $text ); $text = self::replace( '##s', '', $text ); // Links: keep the URL when it differs from the link text. $linked = preg_replace_callback( '#]*?href\s*=\s*(["\'])(.*?)\1[^>]*>(.*?)#is', static function ( $m ) { $href = html_entity_decode( trim( $m[2] ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $label = trim( wp_strip_all_tags( $m[3] ) ); $label = html_entity_decode( $label, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); if ( '' === $href || 0 === stripos( $href, 'javascript:' ) || 0 === strpos( $href, '#' ) ) { return $label; } if ( 0 === stripos( $href, 'mailto:' ) ) { $address = substr( $href, 7 ); return ( '' === $label || $label === $address ) ? $address : $label . ' (' . $address . ')'; } if ( '' === $label || rtrim( $label, '/' ) === rtrim( $href, '/' ) ) { return $href; } return $label . ' (' . $href . ')'; }, $text ); $text = null === $linked ? $text : $linked; // Images: keep meaningful alt text. $text = self::replace( '#]*?alt\s*=\s*(["\'])(.+?)\1[^>]*>#is', '[$2]', $text ); // Block structure to line breaks. $text = self::replace( '##i', "\n", $text ); $text = self::replace( '#]*>#i', "\n- ", $text ); $text = self::replace( '#]*>#i', "\n----------\n", $text ); $text = self::replace( '##i', ' ', $text ); $text = self::replace( '#]*>#i', "\n\n", $text ); $text = wp_strip_all_tags( $text ); $text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); // Normalise whitespace: no trailing spaces, at most one blank line in a row. $text = str_replace( array( "\r\n", "\r", "\xC2\xA0" ), array( "\n", "\n", ' ' ), $text ); $text = self::replace( "/[ \t]+/", ' ', $text ); $text = self::replace( "/ *\n */", "\n", $text ); $text = self::replace( "/\n{3,}/", "\n\n", $text ); return trim( $text ); } /** * Same as preg_replace(), but keeps the subject when the regex engine gives up * (backtrack or recursion limits on very large documents). * * @param string $pattern Pattern. * @param string $replacement Replacement. * @param string $subject Subject. * @return string */ private static function replace( $pattern, $replacement, $subject ) { $result = preg_replace( $pattern, $replacement, $subject ); return null === $result ? $subject : $result; } }