Jeffrmarks Posted July 31, 2016 Share Posted July 31, 2016 Hi I've set up the Featured Products just fine. The owner of the site wants to list the products vertically, so that's done as well. Now he wants just the first 20 words of the product description and "read more" as a link that takes you to the product page. As it stands, this is the featured product code, but I could use some help in displaying the first 20 words. I'm assuming it's a substring. This is the existing code: $featured_prods_content .= '<td style="border-bottom:solid 1px #ccc;"> <div class="outer"> <div id="x"> <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $featured_products['products_image'], $featured_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a> </div> <div id="y"> <span class="name-font"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . $featured_products['products_name'] . '</a></span> <span class="price-font">' . $currencies->display_price($featured_products['products_price'], tep_get_tax_rate($featured_products['products_tax_class_id'])) . '</span><br /><br /> ' . strip_tags($featured_products['products_description'], '<br>') . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . READ_MORE . '</a> </div> </div> </td>'; Link to comment Share on other sites More sharing options...
Bob Terveuren Posted August 1, 2016 Share Posted August 1, 2016 Hi - you could split the description out by space into an array and then restring the first 20 or try using str_word_count something like $words = strip_tags($featured_products['products_description']; $words = str_word_count($words); $desc = array_slice($words,20); $desc = join('',$desc); Then use $desc instead of strip_tags($featured_products['products_description']; in you code Link to comment Share on other sites More sharing options...
MrPhil Posted August 1, 2016 Share Posted August 1, 2016 I think you want str_word_count($words, 1). The default is just to return the number of words. Also consider why the requested limit is set to 20, and what will happen if the words are very short or very long (generally, you are trying to fit a given space). You could look at the character count (split the line at the previous space), but with a proportional font it may be difficult to exactly get the desired length. Don't forget to add an ellipsis (...) and space after the truncated text and before Read More. You may want to look around for examples of "teaser" code to get other ideas. Link to comment Share on other sites More sharing options...
Jeffrmarks Posted August 6, 2016 Author Share Posted August 6, 2016 Hi, I got to a certain point, and I'm stymied here. I added some code I found for a teaser (as mentioned.) Added the call to the routine, but it's not working. I know I'm missing something not too difficult, but I'm not seeing it despite staring at it for a while. Help in debugging it would be greatly appreciated. Thanks Jeff tep_fp_featured_products_limit_text (pd.products_description, $maxchar=200, $wordlength = 40); if ($col === 0) { $featured_prods_content .= '<tr>'; } $featured_prods_content .= '<td style="border-bottom:solid 1px #ccc;"> <div class="outer"> <div id="x"> <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $featured_products['products_image'], $featured_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a> </div> <div id="y"> <span class="name-font"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . $featured_products['products_name'] . '</a></span> <span class="price-font">' . $currencies->display_price($featured_products['products_price'], tep_get_tax_rate($featured_products['products_tax_class_id'])) . '</span><br /><br /> ' . $newtext . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . READ_MORE . '</a> </div> </div> </div> </td>'; if (($col > 2) || ($counter == $num_featured_products)) { $featured_prods_content .= '</tr>'; $col = 0; } } $featured_prods_content .= '</table>'; ?> function tep_fp_featured_products_limit_text ($text, $maxchar=200, $wordlength = 40) { $text = str_replace ("\n", ' ', $text); $text = str_replace ("\r", ' ', $text); $text = str_replace ('<br>', ' ', $text); $text = wordwrap ($text, $wordlength, ' ', true); $text = preg_replace ("/[ ]+/", ' ', $text); $text_length = strlen ($text); $text_array = explode (" ", $text); $newtext = ''; for ($array_key = 0, $length = 0; $length <= $text_length; $array_key++) { $length = strlen ($newtext) + strlen ($text_array[$array_key]) + 1; if ($length > $maxchar) break; $newtext = $newtext . ' ' . $text_array[$array_key]; } return $newtext; Link to comment Share on other sites More sharing options...
MrPhil Posted August 6, 2016 Share Posted August 6, 2016 You can't use pd.products_description in a call to a PHP routine. You need to load it into a variable first. Then, depending on your PHP version, your new routine may or may not be able to modify "$text" in that manner. You may need to rewrite the routine to return the string as a function (which may be cleaner and easier to understand, anyway). Finally, you need to update the existing PHP code to use that revised string. Link to comment Share on other sites More sharing options...
Jeffrmarks Posted August 8, 2016 Author Share Posted August 8, 2016 Thanks so much. I did all the things you suggested, and it's working much better now. I have it looking almost exactly as I want it. I used these statements for the first 20 words. $words = strip_tags($featured_products['products_description'], '<br>'); $words = str_word_count($words,1); $desc = array_slice($words,0,20); $desc = join(' ',$desc); This works great -- except it strips out the punctuation (commas and periods) and any numeric information such as DOB and death. So "Jeff Marks (1940-) needs help." becomes "Jeff Marks needs help" Thoughts on getting this last piece in place?Thanks Link to comment Share on other sites More sharing options...
MrPhil Posted August 9, 2016 Share Posted August 9, 2016 str_word_count() only looks at alphabetic characters. If you want to preserve punctuation and numbers, you'll have to use something else. For example, you might explode() the string on spaces (or preg_split(), if you use tabs or carriage-returns), count up the words and/or characters, and implode() the desired number of words back together into a string. The exact details will depend on whether there are embedded newlines, etc. or it's one long stream. It might be better to process the descriptive text first to remove tags and change all whitespace to just single blanks, and then explode it into words, decide on the length, and implode it back together. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.