0

I only want the first 50 characters of '$output' that has been uploaded to the database to be shown on my page, what is the simplest way for me to do this? If the $output consists of more than 50 characters, I want the text to end on the last word it can without exceeding 50 characters and then put a "..." sort of thing, like a 'read more' section would.

PHP:

    <?php 
        require_once("nbbc/nbbc.php");

        $bbcode = new BBCode;

        $sql = "SELECT * FROM posts ORDER BY id DESC";

        $res = mysqli_query($dbCon, $sql) or die(mysqli_error($dbCon));

        $posts = "";

        if(mysqli_num_rows($res) > 0) {
            while($row = mysqli_fetch_assoc($res)) {
                $id = $row['id'];
                $title = $row['title'];
                $subtitle = $row['subtitle'];
                $content = $row['content'];
                $date = $row['date'];

                $output = $bbcode->Parse($content);

                $posts .= "<div id='post'><h2><a href='view_post.php?pid=$id' target='_blank'>$title</a></h2><h3 id='subtitle'>$subtitle</h3><h3 id='datethingy'><span style='color: #457F2C;'>Last updated:</span> $date</h3><p>$output</p></div>";

            }
            echo $posts;
        }else {
            echo "There are no posts to be displayed.";
        }
    ?>
Ben Pompe
  • 3
  • 5

6 Answers6

1

You can do something like this:

$str50 = substr($mystring, 0, 50);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anoop B.K
  • 1,484
  • 2
  • 17
  • 31
1

The Simplest

You can just use substr to take the first 50 characters.

$output = substr($output, 0, 50);

The Most Efficient

If you're only going to display the first 50 characters, only query the database for the first 50 characters. This is much more efficient as you're transmitting less from the database.

// This is your SQL statement
SELECT LEFT(field, 50) AS excerpt FROM `table` WHERE ...

See this answer for more on only retrieving the first X characters from the database.

Community
  • 1
  • 1
Tim
  • 2,123
  • 4
  • 27
  • 44
1

If your '$ouput' will be shown in a textfield like you said, the simplest way is:

<input type="text" maxlength="50">
0

thats the simplest way with pretty dots after the cut:

echo (strlen($var) > $length ? substr($var, 0, $length).'...' : $var);
Bernhard
  • 1,852
  • 11
  • 19
0

Use one of the following,

$trimmedOutput = substr($output, 0, 50);

First parameter is the string you'd like the substring from. The second parameter is where you'd like the substring to stay from in the original string and the last parameter is where it would like to stop.

If you'd like 60 characters then change the 50 to 60.

Script47
  • 14,230
  • 4
  • 45
  • 66
0

If you don't want to cut a word, the best solution is to use the wordwrap function, like this :

function tokenTruncate($string, $your_desired_width) {
  $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
  $parts_count = count($parts);

  $length = 0;
  $last_part = 0;
  for (; $last_part < $parts_count; ++$last_part) {
    $length += strlen($parts[$last_part]);
    if ($length > $your_desired_width) { break; }
  }

  return implode(array_slice($parts, 0, $last_part));
}

This will split the text into lines that do not exceed 60 characters each, then will pick the first line. Scenarios like when the text is shorter than 60 characters, or has a line break before that limit are also handled.

Lyes BEN
  • 990
  • 4
  • 14