Cutoff SSI_Boardnews by using BBC Tag?

Started by Klozi, August 24, 2010, 03:11:49 AM

Previous topic - Next topic

Klozi

Dear SMF Community,

I'm working on a news portal based on SMF which the basic elements of a blog. In normal case, if a post/news entry is too long (has too more then X words), the news body got cutted off and end with "...".
Is there a way, to cut the news by using a (own) BBC tag like [CUTOFF] to add those "..." on that position and break the news. It would be a nice solution to write some intro text, with some headline based information and the viewer has to click on the "read complete article" link, to read the rest.

Hope you can help me on this out again. :)

Klozi


Klozi


chilly

I'd suggest getting the ssi boardnews to an array and there writing a function that cuts off everything but the first 2-3 sentences. (explode by "." and use only the first 3 entries of that array. of couse you have to check if there are enough entries before trying to use them)

Language Coordinator

Klozi

Thank you,

well I haven't a clue on this to cutoff at the third sentence, because the original cutoff is using a max string position function. Maybe you can help me more in the programming part, please?

The original source to cutoff body looks like:
// If we want to limit the length of the post.
if (!empty($length) && $func['strlen']($row['body']) > $length)
{
$row['body'] = $func['substr']($row['body'], 0, $length);

// The first space or line break. (<br />, etc.)
$cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));

if ($cutoff !== false)
$row['body'] = $func['substr']($row['body'], 0, $cutoff);
$row['body'] .= '...';
}


chilly

// If we want to limit the length of the post.
      if (!empty($length) && $func['strlen']($row['body']) > $length)//checks if $length is not empty and if body length is greater then $length
      {
         $row['body'] = $func['substr']($row['body'], 0, $length);//write $lenght-number of chars to $row['body']

//next is to check wether or not there is a space or a line break within $row['body']. if yes it extracts the text until that sign. if no it doesn't do any more changes.
         // The first space or line break. (<br />, etc.)
         $cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));

         if ($cutoff !== false)
            $row['body'] = $func['substr']($row['body'], 0, $cutoff);
//add "..." to the end of the text
         $row['body'] .= '...';
      }


suggestion:

$sentences = 3; //define how much sentences you want
$tmp = array();
$tmp = explode('. ',$row['body'],$sentences );//used dot + space to identify a new sentence as the dot is used for decimal numbers too.
if(count($tmp)==$sentences )
{
  $row['body'] = $tmp['0'].".".$tmp['1'].".".$tmp['2']."..."; // Replace the original text with the extracted sentences
}else
{
//throw an error
}

Language Coordinator

Klozi

Thank you,

well I tried your second code part and pasted it after the third SQL request of ssi_boardNews. I also replaced the old cutoff code:
// Find the posts.
$request = db_query("
SELECT
m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, t.ID_BOARD, m.ID_MSG, t.locked, m.info
FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m)
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE t.ID_FIRST_MSG IN (" . implode(', ', $posts) . ")
AND m.ID_MSG = t.ID_FIRST_MSG
ORDER BY t.ID_FIRST_MSG DESC
LIMIT " . count($posts), __FILE__, __LINE__);
$return = array();
while ($row = mysql_fetch_assoc($request))
{
$sentences = 2; //define how much sentences you want
$tmp = array();
$tmp = explode('. ',$row['body'],$sentences );//used dot + space to identify a new sentence as the dot is used for decimal numbers too.
if(count($tmp)==$sentences )
{
  $row['body'] = $tmp['0'].".".$tmp['1'].".".$tmp['2']."..."; // Replace the original text with the extracted sentences
}else
{
//throw an error
}

    $row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);


So far so good. For now, nothing has changed and the cutoff works no longer. Maybe I replaced it wrong or I have to use the first code, too?

chilly

please check the content of $row['body'] and the keys of $tmp. check if the content enables the given code to work as expected.
maybe if(count($tmp)==$sentences )  has to be checked as count($tmp) may not return the same as $sentences

Language Coordinator

Klozi

Well I don't get it working. Thank you for your help this far, but could you post more of the code past / the complete part, please?

chilly

   // Find the posts.
   $request = db_query("
      SELECT
         m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
         t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, t.ID_BOARD, m.ID_MSG, t.locked, m.info
      FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m)
         LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
      WHERE t.ID_FIRST_MSG IN (" . implode(', ', $posts) . ")
         AND m.ID_MSG = t.ID_FIRST_MSG
      ORDER BY t.ID_FIRST_MSG DESC
      LIMIT " . count($posts), __FILE__, __LINE__);
   $return = array();
   while ($row = mysql_fetch_assoc($request))
   {
$sentences = 2; //define how much sentences you want
$tmp = array();
$tmp = explode('. ',$row['body'],$sentences );//used dot + space to identify a new sentence as the dot is used for decimal numbers too.
var_dump(count($tmp)); //check the amount given back by "count()"
if(count($tmp)==$sentences )
{
  var_dump($row['body'],$tmp['0'],$tmp['1'],$tmp['2']);//check "body" + the parts of $tmp you want to use.
  $row['body'] = $tmp['0'].". ".$tmp['1'].". ".$tmp['2']."..."; // Replace the original text with the extracted sentences
}else
{
//throw an error
//you have to enter some code here if you want any notification if the function is not able to work
}

    $row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

Language Coordinator

Advertisement: