Jump to content

Cutting off text after certain length


Eiolon

Recommended Posts

On my front page, I link to articles.  I want to use the beginning part of the article as the link but I need it to cut off after a certain amount of characters.  It would then have ... that the visitor could click on to view the entire article.

 

It's actually what PHPFreaks is doing on the main page of their new site.

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/108864-cutting-off-text-after-certain-length/
Share on other sites

<?php
$content = "On my front page, I link to articles.  I want to use the beginning part of the article as the link but I need it to cut off after a certain amount of characters.  It would then have ... that the visitor could click on to view the entire article.

It's actually what PHPFreaks is doing on the main page of their new site.

Thanks for your help.";
$maxchars = 50;
$content = substr($content, 0, $maxchars);
$pos = strrpos($content, " ");
if ($pos>0) {
$content = substr($content, 0, $pos);
}
echo $content."...(read more)";

outputs:

On my front page, I link to articles.  I want to...(read more)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.