function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
echo limit_text('Hello here is a long sentence that will be truncated by the', 5);
Outputs:
Hello here is a long ...
Related Contents:
- Remove empty array elements
- How do I strip all spaces out of a string in PHP? [duplicate]
- Converting an integer to a string in PHP
- How can I get the last 7 characters of a PHP string?
- Split a comma-delimited string into an array?
- Getting the first character of a string with $str[0]
- Convert a comma-delimited string into array of integers?
- Passing base64 encoded strings in URL
- PHP, get file name without file extension
- Remove first 4 characters of a string with PHP
- Multi-line strings in PHP
- How to remove all leading zeroes in a string
- PHP string “contains” [duplicate]
- How to convert array values to lowercase in PHP?
- string sanitizer for filename
- Highlight the difference between two strings in PHP
- Print newline in PHP in single quotes
- How to transform array to comma separated words string? [duplicate]
- Check if URL has certain string with PHP
- What is for Python what ‘explode’ is for PHP?
- How do I remove the last comma from a string using PHP?
- Manipulate a url string by adding GET parameters
- Convert string with dashes to camelCase
- Tetris-ing an array
- PHP remove all characters before specific string
- Get the first letter of each word in a string
- PHP: Best way to extract text within parenthesis?
- Limit String Length
- How can strip whitespaces in PHP’s variable?
- Can you append strings to variables in PHP? [duplicate]
- PHP: Split string [duplicate]
- PHP number: decimal point visible only if needed
- Split camelCase word into words with php preg_match (Regular Expression)
- Most efficient way to get next letter in the alphabet using PHP
- How can I get a hex dump of a string in PHP?
- How to replace Microsoft-encoded quotes in PHP
- PHP remove commas from numeric strings
- Is there any way to detect strings like putjbtghguhjjjanika?
- PHP convert string to hex and hex to string
- PHP – remove tag from string
- How to replace multiple items from a text string in PHP? [duplicate]
- Grab remaining text after last “/” in a php string
- How to convert the time from AM/PM to 24 hour format in PHP?
- Question about strpos: how to get 2nd occurrence of a string?
- Get the string after a string from a string
- How to select first 10 words of a sentence?
- Dollar ($) sign in password string treated as variable
- How to remove accents and turn letters into “plain” ASCII characters? [duplicate]
- Remove quotes from start and end of string in PHP
- If string only contains spaces?