Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit String Length

Tags:

string

php

I'm looking for a way to limit a string in php and add on ... at the end if the string was too long.

like image 783
Belgin Fish Avatar asked Jun 10 '10 23:06

Belgin Fish


People also ask

How do I restrict the length of a string?

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.

Is there a limit to string length C#?

The maximum length of a string literal allowed in Microsoft C is approximately 2,048 bytes.

How do you specify a string length?

The Java String length() method is a method that is applicable for string objects. length() method returns the number of characters present in the string. The length() method is suitable for string objects but not for arrays. The length() method can also be used for StringBuilder and StringBuffer classes.


1 Answers

You can use something similar to the below:

if (strlen($str) > 10)    $str = substr($str, 0, 7) . '...'; 
like image 55
bramp Avatar answered Sep 24 '22 10:09

bramp