Belgin Fish
Belgin Fish

Reputation: 19827

Limit String Length

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

Upvotes: 95

Views: 290122

Answers (13)

Damian
Damian

Reputation: 1

function showAHumanDate( $dateString ){
$toRet = '';
$days = date( "d", strtotime( $dateString ) );
$month = date( "F", strtotime( $dateString ) );
$year = date( "Y", strtotime( $dateString ) );

switch( $days ){
    case '01':
    $string = '1st';
    break;
    case '02':
    $string = '2nd';
    break;
    case '03':
    $string = '3rd';
    break;
    case '21':
    $string = '21st';
    break;
    case '22':
    $string = '22nd';
    break;
    case '23':
    $string = '23rd';
    break;
    case '31':
    $string = '31st';
    break;
    default:
    $string = $days.'th';
}

// strip the month to three letters: 
$month = mb_strimwidth( $month, 0, 3, '' );

$toRet = $string.' '.$month.' '.$year;
return $toRet;

}

Upvotes: 0

Waruna Manjula
Waruna Manjula

Reputation: 3477

 $string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;

Or as function:

function truncate($string, $length, $dots = "...") {
    return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
}

It's been a while since I wrote this answer and I don't actually use this code any more. I prefer this function which prevents breaking the string in the middle of a word using the wordwrap function:

function truncate($string,$length=100,$append="…") {
$string = trim($string);

  if(strlen($string) > $length) {
    $string = wordwrap($string, $length);
    $string = explode("\n", $string, 2);
    $string = $string[0] . $append;
  }

  return $string;
}

Upvotes: 5

Arthur Shlain
Arthur Shlain

Reputation: 1099

function truncateString($string, $maxlength, $ellipsis = false){

    if(mb_strlen($string) <= $maxlength){
        return $string;
    }

    if(empty($ellipsis)){
        $ellipsis = '';
    }

    if($ellipsis === true){
        $ellipsis = '…';
    }

    $ellipsis_length = mb_strlen($ellipsis);

    $maxlength = $maxlength - $ellipsis_length;

    $string = trim(mb_substr($string, 0, $maxlength)) . $ellipsis;

    return $string;

}

http://sandbox.onlinephpfunctions.com/code/968e2fd1e98b60828a12d6dc0b68ec38b3628757

Upvotes: 0

user3815441
user3815441

Reputation: 21

$res = explode("\n",wordwrap('12345678910', 8, "...\n",true))[0];

// $res will be  : "12345678..."

Upvotes: 1

Naeem Ijaz
Naeem Ijaz

Reputation: 825

2nd argument is where to start string and 3rd argument how many characters you need to show

$title = "This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string";
    echo substr($title,0,50);

Upvotes: 12

Sarath Sadasivan Pillai
Sarath Sadasivan Pillai

Reputation: 7091

From php 4.0.6 , there is a function for the exact same thing

function mb_strimwidth can be used for your requirement

<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
//Hello W...
?>

It does have more options though,here is the documentation for this mb_strimwidth

Upvotes: 50

crmpicco
crmpicco

Reputation: 17181

To truncate a string provided by the maximum limit without breaking a word use this:

/**
 * truncate a string provided by the maximum limit without breaking a word
 * @param string $str
 * @param integer $maxlen
 * @return string
 */
public static function truncateStringWords($str, $maxlen): string
{
    if (strlen($str) <= $maxlen) return $str;

    $newstr = substr($str, 0, $maxlen);
    if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " "));

    return $newstr;
}

Upvotes: 1

Sev
Sev

Reputation: 15689

You can use the wordwrap() function then explode on newline and take the first part, if you don't want to split words.

$str = 'Stack Overflow is as frictionless and painless to use as we could make it.';
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';

Source: https://stackoverflow.com/a/1104329/1060423

If you don't care about splitting words, then simply use the php substr function.

echo substr($str, 0, 28) . '...';

Upvotes: 12

Paul Caleb
Paul Caleb

Reputation: 205

$value = str_limit('This string is really really long.', 7);

// This st...

Upvotes: -3

geckob
geckob

Reputation: 8120

In Laravel, there is a string util function for this, and it is implemented this way:

public static function limit($value, $limit = 100, $end = '...')
{
    if (mb_strwidth($value, 'UTF-8') <= $limit) {
        return $value;
    }

    return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}

Upvotes: 1

bramp
bramp

Reputation: 9741

You can use something similar to the below:

if (strlen($str) > 10)
   $str = substr($str, 0, 7) . '...';

Upvotes: 190

PCMShaper
PCMShaper

Reputation: 64

In another way to limit a string in php and add on readmore text or like '...' using below code

if (strlen(preg_replace('#^https?://#', '', $string)) > 30) { 
    echo substr(preg_replace('#^https?://#', '', $string), 0, 35).'&hellip;'; 
}

Upvotes: 0

dlamblin
dlamblin

Reputation: 45321

Do a little homework with the php online manual's string functions. You'll want to use strlen in a comparison setting, substr to cut it if you need to, and the concatenation operator with "..." or "&hellip;"

Upvotes: 3

Related Questions