Forum Moderators: coopster

Message Too Old, No Replies

news posting / cutting of text

         

CodilX

6:18 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



Hi :)

I can't seem to get this done..

I'm trying to make a news script. What I want to do, is that imagine you have a News page, and you can see all the news there. After a few lines of the news text, it would end and there would be a READ MORE link. After clicking that link you would get the full news article.

The issue is that I can't get the text to be cut of correctly. If I for instance choose to cut the text at 300 symbols, it might cut of in the middle of a sentence or even a word, like the way I made:


while($row=mysql_fetch_array($query)){
if(strlen($row['post'])<=300) {
$post = $row['post'];
} else {
$post = substr($row['post'],0,300)." read more";

The ideal solution would be if the text was to be cut off let's say after 300 words and at the end of the upcoming sentence.

Hope someone could help me with this ..

henry0

7:46 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That should do fine
set it at any length, view the usage below function

<?php
// script name sub_str.inc.php
function strtrim($str, $maxlen=125, $elli=NULL, $maxoverflow=15) {
global $CONF;

if (strlen($str) > $maxlen) {

if ($CONF["BODY_TRIM_METHOD_STRLEN"]) {
return substr($str, 0, $maxlen);
}

$output = NULL;
$body = explode(" ", $str);
$body_count = count($body);

$i=0;

do {
$output .= $body[$i]." ";
$thisLen = strlen($output);
$cycle = ($thisLen < $maxlen && $i < $body_count-1 && ($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);
$i++;
} while ($cycle);
return $output.$elli;
}
else return $str;
}

?>

//Usage example
include_once("../sub_str.inc.php");
$str=$txt; // where $txt is your full length text.
$excerpt=nl2br(strtrim($str)); // $excerpt is your excerpt :)

CodilX

8:04 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



thank you so much, works perfectly!

henry0

9:37 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad I could help about a topic I have been helped with before!