Automattic\WooCommerce\EmailEditor\Engine\Renderer

Html2Text::process_whitespace_newlinespublic staticWC 1.0

Remove leading or trailing spaces and excess empty lines from provided multiline text

Method of the class: Html2Text{}

No Hooks.

Returns

String. The fixed text.

Usage

$result = Html2Text::process_whitespace_newlines( $text ): string;
$text(string) (required)
Multiline text with any number of leading or trailing spaces or excess lines.

Html2Text::process_whitespace_newlines() code WC 10.5.0

public static function process_whitespace_newlines( string $text ): string {

	// Remove excess spaces around tabs.
	$result = preg_replace( '/ *\t */im', "\t", $text );
	$text   = null !== $result ? $result : $text;

	// Remove leading whitespace.
	$text = ltrim( $text );

	// Remove leading spaces on each line.
	$result = preg_replace( "/\n[ \t]*/im", "\n", $text );
	$text   = null !== $result ? $result : $text;

	// Convert non-breaking spaces to regular spaces to prevent output issues,
	// do it here so they do NOT get removed with other leading spaces, as they
	// are sometimes used for indentation.
	$text = self::render_text( $text );

	// Remove trailing whitespace.
	$text = rtrim( $text );

	// Remove trailing spaces on each line.
	$result = preg_replace( "/[ \t]*\n/im", "\n", $text );
	$text   = null !== $result ? $result : $text;

	// Unarmor pre blocks.
	$text = self::fix_newlines( $text );

	// Remove unnecessary empty lines.
	$result = preg_replace( "/\n\n\n*/im", "\n\n", $text );

	return null !== $result ? $result : $text;
}