-1

I want to limit the data to only 300 characters to be posted on the page. how i can i place a script under "description"?

Thanks

<?php $result = mysqli_query($con,"SELECT * FROM headlines ORDER BY serial DESC LIMIT 3"); while($row = mysqli_fetch_array($result))  { ?>


<img src="./img/<?php echo $row['picture']?>" height="284px" width="465px" /><br />
<div id="headlinetitle"><a href="./headlines.php?code=<?php echo $row['serial'];?>|productname=<?php echo $row['title']; ?>"><?php echo $row['title']; ?></a></div>

<?php echo $row['description']; ?>
<?php
}
?>
Herbert
  • 21
  • 4
  • possible duplicates: http://stackoverflow.com/questions/4258557/limit-text-length-in-php-and-provide-read-more-link – Krish R Nov 11 '13 at 08:54

3 Answers3

1

Use substr() and replace

echo $row['description']

with

echo substr($row['description'], 0, 300);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

You can use substring like

echo substr($row['description'], 0, 300);
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

I'd use:

if ( !empty($row['description']) ) {
    $description = ( strlen($row['description']) > 300 ? substr($row['description'], 0, 300) . '...' : $row['description'] );
}

The code above will fill $description with either the full description text if its < 300 characters. If it's longer then 300 characters, it will cut off the description text on 300 characters and append '...' to indicate the shortening of the string.

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55