Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2003, 09:39 PM
mtf169's Avatar
mtf169 mtf169 is offline
 
Join Date: Mar 2002
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 05-16-2003, 10:36 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

While it's not exactly what you want, why not just try PHP's http://www.php.net/wordwrap function?
Reply With Quote
  #3  
Old 05-17-2003, 12:31 PM
mtf169's Avatar
mtf169 mtf169 is offline
 
Join Date: Mar 2002
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 05-17-2003, 01:04 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 05-17-2003, 08:06 PM
mtf169's Avatar
mtf169 mtf169 is offline
 
Join Date: Mar 2002
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #6  
Old 05-28-2003, 03:00 PM
mtf169's Avatar
mtf169 mtf169 is offline
 
Join Date: Mar 2002
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Can anyone explain how to use the bbcodeparse2() function? What variables to I have to pass into it?
Reply With Quote
  #7  
Old 05-28-2003, 03:07 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Look at the header for it...
Reply With Quote
  #8  
Old 05-30-2003, 12:04 PM
Kriek's Avatar
Kriek Kriek is offline
 
Join Date: Jul 2002
Location: Florida
Posts: 135
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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); 
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:40 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.12071 seconds
  • Memory Usage 2,251KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete