Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 8th, 2008, 10:42 AM
Authorized User
 
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extracting a flexible amount of text from a field

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 "", 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
__________________
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
 
Old July 25th, 2008, 10:31 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

 
Old July 28th, 2008, 09:57 AM
Authorized User
 
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
Code:
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:
Code:
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
The field is too small to accept the amount of dat Andraw Pro VB 6 2 October 27th, 2008 10:18 AM
Extracting a flexibel amount of text part 2 jmaronilla PHP Databases 0 July 28th, 2008 08:22 PM
Extracting Text Jeff Mason BOOK: Beginning Regular Expressions 1 October 24th, 2006 06:38 AM
How display large amount of dynamic text relaytest49 ASP.NET 2.0 Professional 2 October 6th, 2006 02:19 PM
Help with large amount of text and page breaks rwodabek BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 May 12th, 2006 07:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.