Another teaser question

Thread Solved

Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

Another teaser question

 
0
  #1
Dec 11th, 2007
Hello again,
Can anyone give me any pointers on how to make a 'teaser' or 'preview' of an article.

Thanks
Taffd
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Another teaser question

 
0
  #2
Dec 12th, 2007
Simple.

Take the text of the article as a string, crop the string to a certain size, append "......" to the string and display it.

  1. <?php
  2.  
  3. /*################################################################
  4. # #
  5. # You pass the script a string, a length you want the string #
  6. # to be and the trailing characters, what the function does, #
  7. # is takes the string, finds the last word that will fit into #
  8. # the overall length, and return a string that has been cropped. #
  9. # The function makes sure that a word is not cut in half. #
  10. # #
  11. ##################################################################
  12. # Written by David Speake - david@evilwarus.com #
  13. # Adapted from Oliver Southgate's ASP interpretation #
  14. # http://www.haneng.com/code/VBScript/CropSentence.txt #
  15. ##################################################################
  16. # #
  17. # Examples: #
  18. # #
  19. # $strTemp = "Hello, I am a fish and you are not."; #
  20. # $strTemp = CropSentence($strTemp, 16, "..."); #
  21. # //returns "Hello, I am a..." #
  22. # #
  23. # $strTemp = "Hello, I am a fish and you are not."; #
  24. # $strTemp = CropSentence($strTemp, 17, "..."); #
  25. # //returns "Hello, I am a fish..." #
  26. # #
  27. ################################################################*/
  28.  
  29. function CropSentence ($strText, $intLength, $strTrail)
  30. {
  31. $wsCount = 0;
  32. $intTempSize = 0;
  33. $intTotalLen = 0;
  34. $intLength = $intLength - strlen($strTrail);
  35. $strTemp = "";
  36.  
  37. if (strlen($strText) > $intLength) {
  38. $arrTemp = explode(" ", $strText);
  39. foreach ($arrTemp as $x) {
  40. if (strlen($strTemp) <= $intLength) $strTemp .= " " . $x;
  41. }
  42. $CropSentence = $strTemp . $strTrail;
  43. } else {
  44. $CropSentence = $strText;
  45. }
  46.  
  47. return $CropSentence;
  48. }
  49.  
  50. $strTemp = "Hello, I am a fish and you are not.";
  51. $strTemp = CropSentence($strTemp, 16, "...");
  52. print $strTemp;
  53. ?>
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

Re: Another teaser question

 
0
  #3
Dec 13th, 2007
Thanks FireNet,
As it happens, I found a much simpler way to achieve what I desired, which was to show a teaser when a forum link was rolled over(as here on daniweb).
I used the following code in the title parameter of the topic's (a href) tag, $message being the text I wanted to truncate. It works.

  1. title="<?php echo substr($message, 0, 100); ?>"

I'd like to add a thankyou to all contributors to daniweb. You've helped me enormously with my project, which I hope to complete in the next week or two.

Best Regards,
Taffd
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC