Subject: Extracting a flexible amount of text from a field
Posted By: scottiegirl Post Date: 7/8/2008 10:42:30 AM
I'll try to keep this as simple as possible. Here is the sample table (named News): ID, NewsTitle, NewsItem, NewsAuthor, NewsDate. I'm sure you can extrapolate their data types and possible lengths. My goal on the front page of the site is to print the title, author, date, and a news extract consisting of the first paragraph of the story with a "read more" link. I believe it has to have something to do with LENGTH(str) and LEFT(str,len), but I'm clueless how to accomplish it. If a "flag" character or code, like "<!--break-->", was used, would that help?

Thanks for your help!


HollyAnn
aka Scottiegirl

"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die." - Calvin, Calvin and Hobbs
Reply By: jmaronilla Reply Date: 7/25/2008 10:31:07 PM
scottiegirl,
how r you?   remember me it's john (d chorus in heaven...)
okay, let me give you an example & may be it give a
starting point to figure it out.  good luck!!!

<?php

  $text = "Fans of the 1980 will have little trouble recognizing
  the group's distinctive synthesized sounds and hypnotic dance beats,
  since these two elements are present in almost every song on the
  album; however, the lack of diversity and range is troubling, and I'm
  hoping we see some new influences in the next album.  More
  intelligent lyrics might also help.";

  echo "<p />";

  echo 'Before: <br />' . $text . '<p />';

  $intlen = strlen($text);
  echo 'String length: ' . $intlen . '.<p />';

  $offset = 0;
  $maxchars = 120;

  echo 'String maximum number of characters: ' . $maxchars . '.<p />';

  $lines = ceil($intlen / $maxchars);
  echo 'Total number of lines: ' . $lines . '.<p />';

  $modchars = $intlen % $maxchars;
  echo 'On the last line should at least have ' . $modchars . ' characters.<p />';

  $limit = ($maxchars * $lines) - ( $maxchars + $modchars);

  $minchars = $maxchars - $modchars;

  $lastmaxchars = $modchars + $maxchars;

  echo 'After: <br />';
  
  $line = '';

  for ($i=0; $i < $lines; $i++)
  {
    $substr = substr($text, $offset, $maxchars);
    $strrpos = strrpos($substr, ' ');

    if ($offset < $limit)
    {
      $line .= substr_replace($substr, '<br />', $strrpos);
    }
    else
    {
      $line .= $substr;
    }

    $offset += $strrpos;
  }
  
  echo $line;


?>

cheers,
john

Reply By: scottiegirl Reply Date: 7/28/2008 9:57:01 AM
John,

I must be having a senior moment, and I do apologize, but I can't quite place (d chorus in heaven...) so I'm having difficulty placing you. Sorry again.

I appreciate your programming example. I am, however, not getting the desired results I'm looking for.

1. I don't want to have to dictate how many characters the line is supposed to have.
2. I am getting the following result from your coding:
Before:
Fans of the 1980 will have little trouble recognizing the group's distinctive synthesized sounds and hypnotic dance beats, since these two elements are present in almost every song on the album; however, the lack of diversity and range is troubling, and I'm hoping we see some new influences in the next album. More intelligent lyrics might also help.

String length: 351.

String maximum number of characters: 120.

Total number of lines: 3.

On the last line should at least have 111 characters.

After:
Fans of the 1980 will have little trouble recognizing the group's distinctive synthesized sounds and hypnotic dance
beats, since these two elements are present in almost every song on the album; however, the lack of diversity and
range is troubling, and I'm hoping we see some new influences in the next album. More intelligent lyrics might also hel

(I've obviously tightened up the text in the $text variable.) Also, you'll notice that the text cuts off at the end.
3. I want this to happen:
Before (backend):
<p>Fans of the 1980 will have little trouble recognizing the group's distinctive synthesized sounds and hypnotic dance beats, since these two elements are present in almost every song on the album.</p><p>However, the lack of diversity and range is troubling, and I'm hoping we see some new influences in the next album. More intelligent lyrics might also help.</p>

After (output):
Fans of the 1980 will have little trouble recognizing the group's distinctive synthesized sounds and hypnotic dance beats, since these two elements are present in almost every song on the album.
Read more...

...with a link at "Read more..." going to the full story.

Is this a better explanation?

HollyAnn
aka Scottiegirl

"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die." - Calvin, Calvin and Hobbs

Go to topic 73054

Return to index page 1