Michael Falciglia
Michael Falciglia

Reputation: 1046

PHP/MySQL Limiting characters outputted from a field

When I get a database array, sometimes there is fields with too much data for my results list page, which is suppose to give just a short description. How do I limit the characters count to something like 100.

This is my array for the loop:

<?php
    $i = 1;
    while ($row = mysql_fetch_array($result)) {
?>

This is my echo statement:

<?php echo $row['description']; ?>

Upvotes: 0

Views: 7833

Answers (5)

Michael Nwuzor
Michael Nwuzor

Reputation: 113

Just in case someone is interested in another code snippets on how to limit character output with dots, I use this and it works well for me

CONCAT(SUBSTR(<column_name_here>, 1, 100),'...... Read More') AS Column_Name

Upvotes: 0

Bill Stidham
Bill Stidham

Reputation: 1480

I would do it in the SQL query. It's better to limit the results returned which reduces I/O and network throughput by not returning data from the database you're not going to use. In your SQL query do:

SELECT LEFT(description, 100) AS description
      ,....
FROM ...

Upvotes: 0

Giacomo1968
Giacomo1968

Reputation: 26024

You can use substr like this:

<?php echo substr($row['description'], 0, 100); ?>

But it might be better—depending on your application needs—to do the limiting when making the initial MySQL query using SUBSTR which behaves the same way, but in MySQL.

SELECT SUBSTR(example_field, 1, 100)
FROM example_table
WHERE example_field IS NOT NULL
LIMIT 1
;

That MySQL script basically means return the substring of example_field starting from the first (1) character and going 100 characters in.

This might be better in some cases since if you are limiting text length to 100 characters, why grab the data for fields that might have 1,000+ characters? That would definitely bog down your PHP script in many cases. Handling that in MySQL is the best way to nip it in the bud if your app just needs 100 characters returned.

Upvotes: 4

Sudheesh.R
Sudheesh.R

Reputation: 356

You can try with substr()

<?php echo substr($row['description'],0,100); ?>

OR

function truncate($input, $maxWords, $maxChars)
{
    $words = preg_split('/\s+/', $input);
    $words = array_slice($words, 0, $maxWords);
    $words = array_reverse($words);

    $chars = 0;
    $truncated = array();

    while(count($words) > 0)
    {
        $fragment = trim(array_pop($words));
        $chars += strlen($fragment);

        if($chars > $maxChars) break;

        $truncated[] = $fragment;
    }

    $result = implode($truncated, ' ');

    return $result . ($input == $result ? '' : '...');
}

// try with cuctom function truncate() , it help to cut description by words.

 <?php echo truncate($row['description'],5,200); ?>

Upvotes: 2

sjagr
sjagr

Reputation: 16512

Like the other answers, you should use substr, but you can use it in combination with strpos so that when you shorten the string, you stop after a complete word instead of interrupting a word itself.

$pos = strpos($row['description'], ' ', 100);
echo substr($row['description'], $pos);

Upvotes: 1

Related Questions