Chelsove Posted August 19, 2006 Share Posted August 19, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/ Share on other sites More sharing options...
litebearer Posted August 19, 2006 Share Posted August 19, 2006 this may help a bit...http://www.nstoia.com/toh/technical/teasers/index.phpLite... Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/#findComment-77312 Share on other sites More sharing options...
Jeremysr Posted August 19, 2006 Share Posted August 19, 2006 First store the whole content in a variable, I'll call it $content. Then get the first 350 characters like this:[code=php:0]for ($x = 0; $x < 350; $x++) { $content_teaser .= $content[$x];}[/code]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.[code=php:0]for ($x = len($content_teaser); $content_teaser[$x] != "." || $content_teaser[$x] != "!" || $content_teaser[$x] != "?"; $x--) { $punctuationmark = $x;}[/code]Then finally, get all the characters up to that puncuation mark![code=php:0]for ($x = 0; $x <= $puncuationmark; $x++) { $content_teaser .= $content[$x];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/#findComment-77315 Share on other sites More sharing options...
Chelsove Posted August 19, 2006 Author Share Posted August 19, 2006 Thanks guys, I've got it all to work now! Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/#findComment-77356 Share on other sites More sharing options...
Barand Posted August 20, 2006 Share Posted August 20, 2006 Instead of transferring all the chars then counting backwards from the end check for the punctuation in first loop.[code]<?phpfor ($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);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/#findComment-77517 Share on other sites More sharing options...
sasa Posted August 20, 2006 Share Posted August 20, 2006 maybe[code]<?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;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18041-splitting-text-into-teaser-and-main-part/#findComment-77539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.