Reputation: 23
I'm a little confused here. I'm creating a weblog and I want to limit the number of characters of an article so when the readers click on 'Read more' or '...', they're able to read the full article. I'm searching and try to understand the codes but I'm soooo confused on where to put the codes. I found this code
// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 500) {
// truncate string
$stringCut = substr($string, 0, 500);
// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>';
}
echo $string;
from limit text length in php and provide 'Read more' link. And here's my codes
$articlesql = "SELECT articles.*, categories.category_name, admin.admin_name FROM articles, categories, admin WHERE articles.article_category_id=categories.category_id AND articles.article_admin_id=admin.admin_id";
$articleresult = mysql_query($articlesql);
while($articlerow=mysql_fetch_assoc($articleresult))
echo"
<li>
<span></span>
<div>
<h4><a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>".$articlerow['article_title']."</a></h4>
<span>
Posted in <a href = '#'>".$articlerow['category_name']."</a> by <a href='#'>".$articlerow['admin_name']."</a> on <a href='#'>".$articlerow['article_date']."</a>
</span>
</div>
<a href='article-single.php?id=".$articlerow['article_id']."' title='view details'><img src='images/blog-post-1.jpg' alt=''></a>
<p>".$articlerow['article_content']."</p>
</li>
";
I really need someone to help me. Thank you!
Upvotes: 0
Views: 8612
Reputation: 1298
I would suggest to use printTruncated function from Truncate text containing HTML, ignoring tags.
By using above simple code of strip_tags and setting up substr will not display formatted article content with limitation.
Using printTruncated function as following manner, will display formatted content.
printTruncated(500,$articlerow['article_content'])
at the end of above function, you can set read more link and you can create a JavaScript that will display full content by hiding truncated content and displaying full content.
Upvotes: 0
Reputation: 558
I almost have no idea what's going on in your codes but you only need to limit text from query for example this will get 100 first word
LEFT(article, 100) AS first100
and when you want to show the articles(within a while loop) you should put '...' without breaking a word
$extract = $articlerow['first100'];
$lastSpace = strrpos($extract, ' ');
echo substr($extract, 0, $lastSpace) . '... ';
Upvotes: 0
Reputation: 169
All you need is PHP’s strrpos and substr functions
Write the following query:
$excerpt_query = mysql_fetch_array(mysql_query("SELECT LEFT(article_content, 30) as excerpt FROM articles")) or die(mysql_error());
Now you will have the first 30 characters in the excerpt column of the output. You can happily play around with it using each article id.
$excerpt = $excerpt_query['excerpt'];
$spaceIndex = strrpos($excerpt, ' '); //Finds the last space from the excerpt value.
Now echo substr($excerpt, 0, $spaceIndex); // this will echo the correct string as the excerpt.
This should be the actual read more link:
echo substr($excerpt, 0, $spaceIndex) . '<a href="single.php?id=1">Read More...</a>';
Drop in if you have any queries.
Upvotes: 1
Reputation: 1477
TRY
<?php
$articlesql = "SELECT articles.*, categories.category_name, admin.admin_name FROM articles, categories,
admin WHERE articles.article_category_id=categories.category_id AND articles.article_admin_id=admin.admin_id";
$articleresult = mysql_query($articlesql);
while($articlerow=mysql_fetch_assoc($articleresult)){
$string = strip_tags($articlerow['article_content']);
if (strlen($string) > 500) {
// truncate string
$stringCut = substr($string, 0, 500);
// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' '))."... <a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>Read More</a>";
}
echo "
<li>
<span></span>
<div>
<h4><a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>".$articlerow['article_title']."</a></h4>
<span>
Posted in <a href = '#'>".$articlerow['category_name']."</a> by <a href='#'>".$articlerow['admin_name']."</a> on <a href='#'>".$articlerow['article_date']."</a>
</span>
</div>
<a href='article-single.php?id=".$articlerow['article_id']."' title='view details'><img src='images/blog-post-1.jpg' alt=''></a>
<p>".$string."</p>
</li>
";
}
Upvotes: 3