25

I'm trying to limit my PHP echo to only 200 characters and then if there are any more replace them with "...".

How could I modify the following statement to allow this?

<?php echo $row['style-info'] ?>
Jeremy
  • 1
  • 85
  • 340
  • 366
Craig
  • 773
  • 2
  • 7
  • 10
  • wouldnt it be easier to create a custom function? – badideas Sep 14 '11 at 18:27
  • 1
    possible duplicate of [Trimming a block of text to the nearest word when a certain character limit is reached?](http://stackoverflow.com/questions/708882/trimming-a-block-of-text-to-the-nearest-word-when-a-certain-character-limit-is-re) – brian_d Sep 14 '11 at 18:29
  • https://stackoverflow.com/a/66662165/7186739 – Billu Mar 16 '21 at 19:33

15 Answers15

71

Well, you could make a custom function:

function custom_echo($x, $length)
{
  if(strlen($x)<=$length)
  {
    echo $x;
  }
  else
  {
    $y=substr($x,0,$length) . '...';
    echo $y;
  }
}

You use it like this:

<?php custom_echo($row['style-info'], 200); ?>
  • 2
    This takes way to much space :) – Luka Sep 14 '11 at 18:31
  • 3
    @Luka, But it's a function and a readable one, while our solutions were one-liners for one time use only – Bojan Kogoj Sep 14 '11 at 18:34
  • Hi, Thanks for the response sorry i don't know PHP well enough, where in that statement do i tell it to echo ['style-info']? – Craig Sep 14 '11 at 18:41
  • What do you mean by `'style_info'`? –  Sep 14 '11 at 18:41
  • I'm trying to echo out the contents of a field ['style-info'] from my mysql table but limit it to 200 characters. – Craig Sep 14 '11 at 18:43
  • Okay, then run all of the strings from the table through the `custom_echo` function above. The function doesn't care whether or not its input comes from a database. –  Sep 14 '11 at 18:44
  • Thanks Jack, so simple & handy function. – saadeez Aug 03 '16 at 10:58
39

Not sure why no one mentioned this before -

echo mb_strimwidth("Hello World", 0, 10, "...");
// output: "Hello W..."

More info check - http://php.net/manual/en/function.mb-strimwidth.php

Sandeep
  • 541
  • 4
  • 9
21

Like this:

echo substr($row['style-info'], 0, 200);

Or wrapped in a function:

function echo_200($str){
    echo substr($row['style-info'], 0, 200);
}

echo_200($str);
Yeroon
  • 3,223
  • 2
  • 22
  • 29
5

It gives out a string of max 200 characters OR 200 normal characters OR 200 characters followed by '...'

$ur_str= (strlen($ur_str) > 200) ? substr($ur_str,0,200).'...' :$ur_str;
A J
  • 3,970
  • 14
  • 38
  • 53
Jarnail S
  • 381
  • 2
  • 10
  • 19
5
<?php echo substr($row['style_info'], 0, 200) .((strlen($row['style_info']) > 200) ? '...' : ''); ?> 
Jonnix
  • 4,121
  • 1
  • 30
  • 31
3

This one worked for me and it's also very easy

<?php

$position=14; // Define how many character you want to display.

$message="You are now joining over 2000 current"; 
$post = substr($message, 0, $position); 

echo $post;
echo "..."; 

?>
3
<?php 
    if(strlen($var) > 200){
        echo substr($var,0,200) . " ...";
    }
    else{
        echo $var;
    }
?>
Billu
  • 2,733
  • 26
  • 47
2

this is most easy way for doing that

//substr(string,start,length)
substr("Hello Word", 0, 5);
substr($text, 0, 5);
substr($row['style-info'], 0, 5);

for more detail

https://www.w3schools.com/php/func_string_substr.asp

http://php.net/manual/en/function.substr.php

wasimv09
  • 146
  • 6
1

more flexible way is a function with two parameters:

function lchar($str,$val){return strlen($str)<=$val?$str:substr($str,0,$val).'...';}

usage:

echo lchar($str,200);
gjerich
  • 369
  • 3
  • 6
1
function TitleTextLimit($text,$limit=200){
 if(strlen($text)<=$limit){
    echo $text;
 }else{
    $text = substr($text,0,$limit) . '...';
    echo $text;
 }
Maciej Paprocki
  • 1,230
  • 20
  • 29
Ali Umair
  • 690
  • 7
  • 10
  • 3
    Could you please add an explanation of why and how this code answers the question. It would be much more useful than just this wordless dump of code. – trincot Nov 20 '15 at 11:40
  • I fixed the formatting, also naming of function is not best. It's not better then first answer too. – Maciej Paprocki Jan 11 '16 at 15:11
1
string substr ( string $string , int $start [, int $length ] )

http://php.net/manual/en/function.substr.php

Luka
  • 321
  • 1
  • 4
  • 21
0
echo strlen($row['style-info'])<=200 ? $row['style-info'] : substr($row['style-info'],0,200).'...';
gjerich
  • 369
  • 3
  • 6
0

Try This:

echo ((strlen($row['style-info']) > 200) ? substr($row['style-info'],0,200).'...' : $row['style-info']);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Rafiqul Islam
  • 931
  • 11
  • 14
0

In this code we define a method and then we can simply call it. we give it two parameters. first one is text and the second one should be count of characters that you wanna display.

function the_excerpt(string $text,int $length){
    if(strlen($text) > $length){$text = substr($text,0,$length);}
    return $text; 
}
  • 2
    Please add an explanation to your code: What does it do, how does it work, and how does it solve OPs problem. Code only answers can lead to [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming), and you risk getting downvotes. – Max Vollmer Dec 10 '19 at 12:20
0
echo strlen($row['style-info']) > 200) ? substr($row['style-info'], 0, 200)."..." : $row['style-info'];
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57