Automattic\WooCommerce\Blocks\BlockTypes
ProductSummary::trim_characters
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() ProductSummary::trim characters code WC 10.8.1
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 );
}