How to limit string length in PHP ? - GeeksforGeeks

21 Oct, 2021
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. 
Approach 1 (Using for loop): The str_split() function can be used to convert the specified string into the array object. The array elements are individual characters stored at individual indices. 
str_split($string)
Parameters: 
$string – The string to split into array
Example: A for loop iteration is performed over the array object until the desired length. The characters are displayed until the length is equivalent to the desired number of characters. 
PHP
<?php    $string = "Geeks for geeks is fun";    echo("Original String : ");      echo($string . "\n");    echo("Modified String : ");      $arr = str_split($string);      for($i = 0; $i < 10; $i++) {        print($arr[$i]);    }?>
Original String : Geeks for geeks is fun
Modified String : Geeks for 
Approach 2 (Using mb_strimwidth() function): 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
<?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!!
Approach 3 (Using substr() method ): The substr() function can be used to extract the specified string within the specified limits. The starting and ending indexes are specified and the equivalent number of characters between the start and end length are extracted. 
substr(string, start, end)
Parameters: 
string – The string to extract the substring from.
start – The starting index to extract the string from.
end – The ending index to extract the string.
Example:
PHP
<?php    $string = "Geeks for geeks is fun";    echo("Original String : ");      echo($string . "\n");    echo("Modified String : ");      if (strlen($string) > 10)       $new_string = substr($string, 0, 10) . '!!!';    echo($new_string . "\n");?>
Output:
Original String : Geeks for geeks is fun
Modified String : Geeks for !!!
PHP-function
PHP-Questions
PHP-string
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
How to generate PDF file using PHP ?
Different ways for passing data to view in Laravel
How to Install php-curl in Ubuntu ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?