vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   first x words (https://vborg.vbsupport.ru/showthread.php?t=53023)

mtf169 05-16-2003 09:39 PM

first x words
 
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):

PHP Code:

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 "...":
PHP Code:

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 [URL] 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:
PHP Code:

$pagetext ereg_replace(
    
"\[URL]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/URL]",
    
"<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

PHP Code:

function chopPost($varb$num) {
    
$dnum intval($num);
    if (
strlen($varb) > $dnum) {
        
$nvarb substr($varb0$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!
PHP Code:

echo chopPost($post50); 



All times are GMT. The time now is 08:29 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01169 seconds
  • Memory Usage 1,741KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete