Guest Posted August 31, 2015 Share Posted August 31, 2015 Just thought I would share. I found that when testing if one has a short paragraph <p> within the first 20 words of their product description, the default is to remove the tag and not insert a space causing merged words. Default 234BS version. Within product_listing.php: $prod_list_contents .= ' <p class="group inner list-group-item-text">' . strip_tags($listing['products_description'], '<br>') . '…</p><div class="clearfix"></div>'; the following does a simple preg_match search n replace with a space. $prod_list_contents .= ' <p class="group inner list-group-item-text">' . strip_tags(preg_replace ('/<[^>]*>/', ' ', $listing['products_description'])) . '…</p><div class="clearfix"></div>'; Link to comment Share on other sites More sharing options...
Guest Posted September 1, 2015 Share Posted September 1, 2015 Just wanted to make clear that the base code allows for <br> to allow for line spacing. One could easily allow strip_tags to leave both the <p> and <br> then just replacing the <p> with spaces. That is not what I needed. Link to comment Share on other sites More sharing options...
MrPhil Posted September 6, 2015 Share Posted September 6, 2015 If you really want to do this well, I would suggest writing a tep_teaser($text [,$maxlen]) function. You may not want to cut in the middle of a word, and if your text is UTF-8, you have to be careful not to cut in the middle of a multibyte character! I presume the text here ($listing['products_description']) has already been shortened somewhere else, as it doesn't seem to be done here. The original code strips out all tags except <br>, which probably doesn't belong in a teaser anyway. Note that your change replaces all tags by a space, and then still calls strip_tags(), which is now redundant! One or the other should do. A teaser function could replace certain tags by spaces, and then strip the rest (except maybe for <i> and <b>, which could be usefully kept in a teaser). It could avoid the problem of multiple tags in one place producing a long string of blanks (which will usually be condensed to a single space by the browser, so that's probably not a major problem, except if those blanks count towards the character count length). It might even end the teaser early at the end of a paragraph or at a break or other block-level tag, before the word or character count has been satisfied. There's lots of things that could be done to produce a slick looking teaser. Link to comment Share on other sites More sharing options...
Guest Posted September 6, 2015 Share Posted September 6, 2015 @@MrPhil Thanks for the catch on the strip_tag redundancy. For some reason, I am not sure why pasted that into the post as I removed the strip_tag from my system completely. $prod_list_contents .= ' <p class="group inner list-group-item-text">' . preg_replace ('/<[^>]*>/', ' ', $listing['products_description']) . '…</p><div class="clearfix"></div>'; As for your other points, the sql query only pulls the first 20 words. I didn't want to get into making this more complicated than what was already being done (was focused on merged words). The following is a function that is used with both the recently_viewed and the featured_products modules. It does some of what you have mentioned function tep_fp_featured_products_limit_text ($text, $maxchar, $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; } // function tep_limit_text Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.