View Full Version : How to shorten text...

01-23-2008, 12:53 PM
I'm getting some content from database and I want to display just some part of text on the front page and "read more" after that. How to do that? I tried to use substr(); but it was swallowing other results too.

01-23-2008, 01:42 PM
Hi there,

Could you post some code?

I don't understand what you mean by "swallowing other results too".

substr just does what it says on the tin!

01-23-2008, 01:59 PM
I was using this: $content = substr($content, 0, 200);
When there are several results on a page, if fist one is more than 200 chars, it was shorting fist post, but it also wasn't displaying other posts below

if($_GET['page']) // Is page defined?


$page = $_GET['page']; // Set to the page defined


$page = 1; // Set to default page 1

$max = $settings['max'];
$cur = (($page * $max) - $max);
$start = ($page - 1)*$limit;

$query = "SELECT * FROM news ORDER BY ID DESC LIMIT $cur, $max";
$counttotal = mysql_query("SELECT * FROM news ") or die("HTTP://WWW.TUTORIALS.GE"); // select all records

$counttotal = mysql_num_rows($counttotal); // count records
//Execute the query.
$result = mysql_query($query) or die("NO SUCH PAGE!");
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.

//nl2br() translates all newlines ('n') as HTML tags.
$content = nl2br($content);
if($settings['mod_rewrite'] == '1'){
echo "<table><tr><td><b><a href='http://localhost/$id/$title.html'><br>$title</a></b></td></tr>
<tr><td><small><i>Written by <b>$author</b> on $date</i></small><br />Category: <a href='category/$category'>$category</a></td></tr>

echo "<table><tr><td><b><a href='view.php?action=view&id=$id'><br>$title</a></b></td></tr>
<tr><td><small><i>Written by <b>$author</b> on $date</i></small><br />Category: <a href='view.php?action=viewCat&category=$category'>$category</a></td></tr>

$total_pages = ceil($counttotal / $max);
if($page > 1){ // is the page number more than 1?

$prev = ($page - 1); // if so, do the following. take 1 away from the current page number

echo "<a href='?page=" . $prev . "'>&#171; Previous </a>"; // echo a previous page link

for($i = 1; $i <= $total_pages; $i++) // for each page number


if($page == $i) // if this page were about to echo = the current page


echo "<b>" . $i . "</b>"; // echo the page number bold

} else {

echo ('<a href="?page=' . $i .'"> ' . $i . ' </a>');


if($page < $total_pages){ // is there a next page?

$next = ($page + 1); // if so, add 1 to the current

echo '<a href="?page='.$next.'">Next &#187;</a>'; // echo the next page link


01-23-2008, 02:31 PM
Ok, I see what you are trying to do; I'm looking at the while loop ... what do you mean by "not displaying" exactly? Do you mean that only one table is being displayed?

name _F1
01-23-2008, 02:32 PM
I can't see a substr anywhere in your code example, so I'm not sure quite what you mean, but...

Try something like:

echo substr($content,0,200); //Echo the first 200 characters
if (strlen($content) > 200) echo "...\n<a href="?read=$id">Read more!</a>"; //Print an ellipsis and a "Read more" link if the content exceeds the amount posted.

01-23-2008, 04:41 PM
Here's what I built for this type of a situation. I use PHP to see if the content is more than 150 characters. If so, it takes the first 150 characters, looks for a space from the end of the content, and only takes up to that character (so it doesn't chop up a word). Then, I throw on a "..." to the end...you could also throw on a link to "read more" if you want. If the content is <= 150, it just shows the full description.

$view_description = '';

if (strlen($description) > 150) {
$view_description = substr($description, 0, 150);
$temp = strrpos($view_description, ' ');
$view_description = substr($description, 0, $temp) . '...';
} else {
$view_description = $description;

01-23-2008, 06:12 PM
Thanks everyone, but especially JohnDubya
I did it :)

01-23-2008, 08:12 PM
Thanks everyone, but especially JohnDubya
I did it :)

Way to go! :thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum