/**
* Limit the number of words in a string.
*
* @param int $words Words limit
* @param string $append Text to append to the string IF it gets truncated
*
* @return self Returns instance of The Strings class.
*/
public function wordsLimit(int $words = 100, string $append = '...'): self
Examples
// Get the number of words in a string with predefined limit settings
$string1 = Strings::create('SG-1 returns from an off-world mission to P9Y-3C3')->wordsLimit();
// Get the number of words in a string with limit 3
$string2 = Strings::create('SG-1 returns from an off-world mission to P9Y-3C3')->wordsLimit(3);
// Get the number of words in a string with limit 3 and append 'read more...'
$string3 = Strings::create('SG-1 returns from an off-world mission to P9Y-3C3')->wordsLimit(3, 'read more...');
echo $string1;
echo $string2;
echo $string3;
The above example will output:
SG-1 returns from an off-world mission to P9Y-3C3...
SG-1 returns from...
SG-1 returns fromread more...
Getting Started
Methods
- after Return the remainder of a string after the first occurrence of a given value.
- afterLast Return the remainder of a string after the first occurrence of a given value.
- append Append the given values to the string
- at Returns the character at $index, with indexes starting at 0.
- base64Decode Decodes data encoded with MIME base64
- base64Encode Encodes data with MIME base64.
- before Get the portion of a string before the first occurrence of a given value.
- beforeLast Get the portion of a string before the last occurrence of a given value.
- between Get the portion of a string between two given values.
- camel Convert a string to camel case.
- capitalize Converts the first character of every word of string to upper case and the others to lower case.
- chars Returns an array consisting of the characters in the string.
- charsFrequency Get chars usage frequency array.
- contains Determine if a given string contains a given substring.
- containsAll Determine if a given string contains all array values.
- containsAny Determine if a given string contains any of array values.
- copy Creates a new Strings object with the same string.
- count Returns the length of the string, analog to length().
- count Returns the number of occurrences of $substring in the given string.
- crc32 Generate the crc32 polynomial from the input string.
- create Create a new stringable object from the given string.
- echo Echo the string.
- endsWith Determine if a given string ends with a given substring.
- finish Cap a string with a single instance of a given value.
- firstSegment Get the first segment from a string based on a delimiter.
- format Return the formatted string.
- getEncoding Get character encoding.
- getIterator Returns a new ArrayIterator, thus implementing the IteratorAggregate interface.
- hash Generate a hash string from the input string.
- increment Add's _1 to a string or increment the ending number to allow _2, _3, etc.
- indexOf Returns the index of the first occurrence of $needle in the string, and false if not found. Accepts an optional offset from which to begin the search.
- indexOfLast Returns the index of the last occurrence of $needle in the string, and false if not found.
- insert Inserts $substring into the string at the $index provided.
- isAlpha Returns true if the string contains only alphabetic chars, false otherwise.
- isAlphanumeric Returns true if the string contains only alphabetic and numeric chars, false otherwise.
- isAscii Returns true if the string contains ASCII, false otherwise.
- isBase64 Returns true if the string is base64 encoded, false otherwise.
- isBlank Returns true if the string contains only whitespace chars, false otherwise.
- isBoolean Determine whether the string is Boolean.
- isDigit Returns true if the string contains only digit chars, false otherwise.
- isEmail Returns true if the string is email and it is valid, false otherwise.
- isEmpty Returns true if the string is url and it is valid, false otherwise.
- isEqual Determine whether the string is equals to $string.
- isFalse Determine whether the string is Boolean and it is FALSE.
- isHexadecimal Returns true if the string contains only hexadecimal chars, false otherwise.
- isHTML Determine whether the string is IP and it is a valid IP address.
- isIP Determine whether the string is IP and it is a valid IP address.
- isJson Returns true if the string is JSON, false otherwise.
- isLower Returns true if the string contains only lower case chars, false otherwise.
- isMAC Determine whether the string is MAC address and it is a valid MAC address.
- isNumeric Returns true if the string is a number or a numeric strings, false otherwise.
- isPrintable Returns true if the string contains only printable (non-invisible) chars, false otherwise.
- isPunctuation Returns true if the string contains only punctuation chars, false otherwise.
- isSerialized Returns true if the string contains only punctuation chars, false otherwise.
- isSimilar Check if two strings are similar.
- isTrue Determine whether the string is Boolean and it is TRUE.
- isUpper Returns true if the string contains only upper case chars, false otherwise.
- isUrl Returns true if the string is url and it is valid, false otherwise.
- kebab Convert a string to kebab case.
- lastSegment Get the last segment from a string based on a delimiter.
- length Return the length of the given string.
- limit Limit the number of characters in a string.
- lines Get array of individual lines in the string.
- lower Convert the given string to lower-case.
- md5 Generate a md5 hash string from the input string.
- move Move substring of desired $length to $destination index of the original string.
- normalizeNewLines Standardize line endings to unix-like.
- normalizeSpaces Normalize white-spaces to a single space.
- offsetExists Returns whether or not a character exists at an index.
- offsetGet Returns the character at the given index.
- offsetSet Implements part of the ArrayAccess interface, but throws an exception when called.
- offsetUnset Implements part of the ArrayAccess interface, but throws an exception when called.
- padBoth Pad both sides of a string with another.
- padLeft Pad the left side of a string with another.
- padRight Pad the right side of a string with another.
- pipe Passes the strings to the given callback and return the result.
- prepend Prepend the given values to the string.
- quotesToEntities Convert single and double quotes to entities.
- random Creates a random string of characters.
- reduceSlashes Reduces multiple slashes in a string to single slashes.
- repeat Returns a repeated string given a multiplier.
- replace Replace the given value in the given string.
- replaceArray Replace a given value in the string sequentially with an array.
- replaceDashes Replace all dashes characters in the string with the given value.
- replaceFirst Replace the first occurrence of a given value in the string.
- replaceLast Replace the last occurrence of a given value in the string.
- replaceNonAlpha Replace none alpha characters in the string with the given value.
- replaceNonAlphanumeric Replace none alphanumeric characters in the string with the given value.
- replacePunctuations Replace all dashes characters in the string with the given value.
- reverse Reverses string.
- segment Get a segment from a string based on a delimiter.
- segments Get array of segments from a string based on a delimiter.
- setEncoding Set the character encoding.
- sha1 Generate a sha1 hash string from the input string.
- sha256 Generate a sha256 hash string from the input string.
- shuffle Randomly shuffles a string.
- similarity Calculate the similarity between two strings.
- snake Convert a string to snake case.
- start Begin a string with a single instance of a given value.
- startsWith Determine if a given string starts with a given substring.
- stripQuotes Removes single and double quotes from a string.
- stripSpaces Strip all whitespaces from the given string.
- studly Convert a string to studly caps case.
- substr Return the length of the given string.
- toArray Return Strings object as array based on a delimiter.
- toBoolean Returns a boolean representation of the given logical string value.
- toFloat Return Strings object as float.
- toInteger Return Strings object as integer.
- toString Return Strings object as string.
- trim Strip whitespace (or other characters) from the beginning and end of a string.
- trimLeft Strip whitespace (or other characters) from the beginning of a string.
- trimRight Strip whitespace (or other characters) from the end of a string.
- trimSlashes Removes any leading and trailing slashes from a string.
- ucfirst Converts the first character of a string to upper case and leaves the other characters unchanged.
- upper Convert the given string to upper-case.
- words Get words from the string.
- wordsCount Get words count from the string.
- wordsFrequency Get words usage frequency array.
- wordsLimit Limit the number of words in a string.
- wordsSortAsc Sort words in string ascending.
- wordsSortDesc Sort words in string descending.