PDA

View Full Version : first x words


mtf169
05-16-2003, 09:39 PM
I've written a some scripts to pull out posts from specific forums to display on non-vb pages, but am having trouble cropping the sentence for display on the article listing page. If you want to see my problem, go here http://www.numbmonkey.com. Basically, I want the title and the first x words displayed. I've tried two different functions to crop at x characters or x words, but they have a tendancy to cut URL links in half. How can I crop after so many words with busting the vbulletin URL tags.

Here are the two scripts I tried (first one adds a nice "..." after crop):


function CropSentence ($strText, $intLength, $strTrail) {
$wsCount = 0;
$intTempSize = 0;
$intTotalLen = 0;
$intLength = $intLength - strlen($strTrail);
$strTemp = "";
if (strlen($strText) > $intLength) {
$arrTemp = explode(" ", $strText);
foreach ($arrTemp as $x) {
if (strlen($strTemp) <= $intLength) $strTemp .= " " . $x;
}
$CropSentence = $strTemp . $strTrail;
} else {
$CropSentence = $strText;
}
return $CropSentence;
}


and the second one crops by words but doesn't add the "...":

function firstwords($str,$wordcount) {
$words=preg_split('/([\s.,;]+)/',$str,$wordcount+1,PREG_SPLIT_DELIM_CAPTURE);
array_pop($words);
return(implode('',$words));
}


Any help on this would be greatly appreciated!

Thanks,
-Matt

filburt1
05-16-2003, 10:36 PM
While it's not exactly what you want, why not just try PHP's http://www.php.net/wordwrap function?

mtf169
05-17-2003, 12:31 PM
Thanks for the idea of using wordwrap. It sort of worked by keeping the long URL's from breaking the design, but it didn't fix them not getting converted into a usable link. In my text format function, I have it convert the vbulleting tag into an html link. I think that the way my cropsentence function (seen in my first post) is working is preventing this conversion from working correctly.

Here's what I use to convert the [URL] into a link:

$pagetext = ereg_replace(
"\[URL]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\",
"<a href=\"\\1\">\\1</a>", $pagetext);


Any ideas? You can see what's broken at http://www.numbmonkey.com

filburt1
05-17-2003, 01:04 PM
Side note: use the preg (preg_replace) instead of the ereg (ereg_replace) functions for speed.

If these non-vB pages have global.php included, you can just use the bbcodeparse2() function to do all the work. :)

mtf169
05-17-2003, 08:06 PM
Oh man ... you gotta be kidding. Here I am banging my head against the desk trying to figure out how to do this and I could have been using bbcodeparse2? Can you give any guidance on how to use it? I found the function but am not sure what I have to pass into it?

Thanks!

-Matt

mtf169
05-28-2003, 03:00 PM
Can anyone explain how to use the bbcodeparse2() function? What variables to I have to pass into it?

filburt1
05-28-2003, 03:07 PM
Look at the header for it...

Kriek
05-30-2003, 12:04 PM
function chopPost($varb, $num) {
$dnum = intval($num);
if (strlen($varb) > $dnum) {
$nvarb = substr($varb, 0, $dnum);
}
elseif(strlen($varb) < $dnum) {
$nvarb = $varb;
}
return $nvarb;
}
If the string contains characters that exceed the limit, the function returns the number of characters specified. If the string contains characters less than the limit, the function returns the full string. Naturally $post should contain the forum post and 50 represents the amount of characters you wish to display. Hope this helps, Enjoy!
echo chopPost($post, 50);