Jump to content


Photo

Splitting text into teaser and main part


  • Please log in to reply
5 replies to this topic

#1 Chelsove

Chelsove
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 August 2006 - 05:15 PM

Hi,

I've got a problem with strings. We've got a lot of articles/stories stored in a database, but now I need to separate the text into two parts, that's going to be stored in different fields in the database (this is just how the content management system works). The first part is supposed to be fairly short, as it's just a teaser, and the rest of the text I'd put in the other field.

I was thinking that if say, the initial text could be max 350 characters long or 60-70 words, then I could search backwards from there until I hit some punctuation like ".", "?" or "!".

But I've had no luck finding any kind of functions on php.net that seems to suit me. I'm not an experienced php coder either, so I don't know my way around that well.

Anyone who can help?

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 19 August 2006 - 06:53 PM

this may help a bit...

http://www.nstoia.co...asers/index.php

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 19 August 2006 - 07:20 PM

First store the whole content in a variable, I'll call it $content. Then get the first 350 characters like this:

for ($x = 0; $x < 350; $x++) {
     $content_teaser .= $content[$x];
}

Then to find the position of the last puncutation mark, use a for loop starting at the end and going backwards until it finds a puncuatation mark.

for ($x = len($content_teaser); $content_teaser[$x] != "." || $content_teaser[$x] != "!" || $content_teaser[$x] != "?"; $x--) {
     $punctuationmark = $x;
}

Then finally, get all the characters up to that puncuation mark!

for ($x = 0; $x <= $puncuationmark; $x++) {
     $content_teaser .= $content[$x];
}


#4 Chelsove

Chelsove
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 August 2006 - 09:45 PM

Thanks guys, I've got it all to work now!

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 20 August 2006 - 10:47 AM

Instead of transferring all the chars then counting backwards from the end check for the punctuation in first loop.

<?php
for ($x = 0; $x < 350; $x++) {
     $content_teaser .= $content{$x};
     if ($content{$x} == '.' || $content{$x} == '?' || $content{$x} == '!') {
         $punc = $x+1;
     }
}
$content_teaser = substr($content,0,$punc);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 August 2006 - 01:20 PM

maybe
<?php
$p = array('.', '!', '?');
$max = 350;
$x = min($max, strlen($content));
while (!in_array($content[--$x], $p) and $x >= 0);
if ($x >= 0) $out=substr($content, 0, $x+1); 
else $out = 'Err'; // error 
echo $out;
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users