| | |
Another teaser question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
Hello again,
Can anyone give me any pointers on how to make a 'teaser' or 'preview' of an article.
Thanks
Taffd
Can anyone give me any pointers on how to make a 'teaser' or 'preview' of an article.
Thanks
Taffd
Simple.
Take the text of the article as a string, crop the string to a certain size, append "......" to the string and display it.
Take the text of the article as a string, crop the string to a certain size, append "......" to the string and display it.
PHP Syntax (Toggle Plain Text)
<?php /*################################################################ # # # You pass the script a string, a length you want the string # # to be and the trailing characters, what the function does, # # is takes the string, finds the last word that will fit into # # the overall length, and return a string that has been cropped. # # The function makes sure that a word is not cut in half. # # # ################################################################## # Written by David Speake - david@evilwarus.com # # Adapted from Oliver Southgate's ASP interpretation # # http://www.haneng.com/code/VBScript/CropSentence.txt # ################################################################## # # # Examples: # # # # $strTemp = "Hello, I am a fish and you are not."; # # $strTemp = CropSentence($strTemp, 16, "..."); # # //returns "Hello, I am a..." # # # # $strTemp = "Hello, I am a fish and you are not."; # # $strTemp = CropSentence($strTemp, 17, "..."); # # //returns "Hello, I am a fish..." # # # ################################################################*/ 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; } $strTemp = "Hello, I am a fish and you are not."; $strTemp = CropSentence($strTemp, 16, "..."); print $strTemp; ?>
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
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.
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
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.
PHP Syntax (Toggle Plain Text)
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
![]() |
Other Threads in the PHP Forum
- Previous Thread: Reporting Utility
- Next Thread: php mysql question
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube





