Formulize

Forms, workflows, and reporting. 100% free and open source.

Version 7.3 out now!How to installHow to update

Download .zip View on GitHub Join us on Slack Follow on Twitter

printSmart( (string) $value, (int) $chars = 35 )

Location

/modules/formulize/include/functions.php

Description

Truncates a string to a given length, and appends “…” onto the end if necessary.

Take care when using this function with text that contains HTML tags, since you can end up removing the closing tags, and the entire page may not render in the browser! It is probably best to remove the HTML tags first, and convert any HTML special chars.

Take care when using this function with text that contains language tags (ie: [en]English text[/en][fr]texte français[/fr]), since you can cut off the closing tags and the entire remainder of the page may be removed by the language tag processing function. Use the trans function on the text first.

This function is multibyte aware and should operate OK with text that contains emojis, and Chinese, Japanese, and Korean characters.

Parameters

$value - the text to truncate.
$chars - Optional. The number of characters to keep, before adding “…”, defaults to 35.

Return Values

Returns the truncated string, or the original string if it is shorter than the number of chars.

Examples

// Shorten a string
$string = "Now is the time for all good men to come to the aid of the party";
$string = printSmart($string);
// Shorten a string only if it is longer than 100 characters
$string = "Now is the time for all good men to come to the aid of the party";
$string = printSmart($string, 100);
// Remove HTML tags and convert special chars before running text through printSmart
$string = htmlspecialchars_decode(strip_tags("<strong>Now&#039;s the time for<br>all good men to come to the aid of the party</strong>"), ENT_QUOTES);
$string = printSmart($string);
// Translate langauge strings before before running text through printSmart
$string = trans("[en]Now is the time for all good men to come to the aid of the party[/en][fr]Il est maintenant temps pour tous les bons hommes de venir en aide au parti politique[/fr].");
$string = printSmart($string);