How to use mb_strimwidth() function In PHP
The mb_strimwidth function is used to get truncated string with specified width. It takes as input the string and the required number of characters. The characters after it are appended with an β!!β string. It returns the string with the trimmed length.
mb_strimwidth(string, start, width, trim_pt)
Parameters:
- string β The string which is to be trimmed.
- start β The start position offset.
- width β The desired trim length.
- trim_pt β The string that can be added to truncate the length of string with.
Example:
<?php
// Declaring original string
$string = "Geeks for geeks is fun";
echo("Original String : ");
echo($string . "\n");
// Trimming length of string
$new_string = mb_strimwidth($string, 0, 11, "!!");
print("Modified String : ");
print($new_string);
?>
Output
Original String : Geeks for geeks is fun
Modified String : Geeks for!!
How to limit string length in PHP ?
A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted.
Table of Content
- Using for loop
- Using mb_strimwidth() function
- Using substr() method
- Using Regular Expressions with preg_match()
- Using substr_replace() for truncation with ellipsis