Automattic\WooCommerce\Blocks\BlockTypes

ProductSummary::trim_charactersprivateWC 1.0

Trim characters to a specified length.

Method of the class: ProductSummary{}

No Hooks.

Returns

String. Trimmed text.

Usage

// private - for code of main (parent) class only
$result = $this->trim_characters( $text, $max_length, $count_type );
$text(string) (required)
Text to trim.
$max_length(int) (required)
Maximum length of the text.
$count_type(string) (required)
What is being counted. One of 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.

ProductSummary::trim_characters() code WC 9.9.3

private function trim_characters( $text, $max_length, $count_type ) {
	$pure_text = wp_strip_all_tags( $text );
	$trimmed   = mb_substr( $pure_text, 0, $max_length );

	if ( 'characters_including_spaces' === $count_type ) {
		return $trimmed;
	}

	preg_match_all( '/([\s]+)/', $trimmed, $spaces );
	$space_count = ! empty( $spaces[0] ) ? count( $spaces[0] ) : 0;
	return mb_substr( $pure_text, 0, $max_length + $space_count );
}