johnstephens Posted December 2, 2015 Share Posted December 2, 2015 Hi, Suppose I have a field called "body" that uses the CNKEditor fieldtype. On the parent page, I want to list a bunch of children and show an automatic excerpt of the body, with my own custom trailing text that links to the full content.Is there an easy way to generate a short summary of the "body" field that yields valid (X)HTML? This is such a common pattern, I imagine there's a module for it, but I searched the module directory without success; the terms I used offered no results.By way of comparison, this Textpattern plugin is mostly what I have in mind: rvm_substr. Link to comment Share on other sites More sharing options...
kongondo Posted December 2, 2015 Share Posted December 2, 2015 Simplest way I can think of is to use PHP substr and strip_tags. The latter is very greedy so you need to tell it what to avoid [allowable tags]...(it get could messy very quickly). Anyhow, this is how we do it in the Blog module using those two PHP functions. 2 Link to comment Share on other sites More sharing options...
johnstephens Posted December 3, 2015 Author Share Posted December 3, 2015 Good enough for me—thank you! Link to comment Share on other sites More sharing options...
SiNNuT Posted December 3, 2015 Share Posted December 3, 2015 Sidenote In my experience simply truncating text to a hard limit to make summaries or intro text,rarely gives good results. If possible i would always choose for a designated summary/intro field. This just gives you more control and options and will probably save some headaches in the long run. 5 Link to comment Share on other sites More sharing options...
johnstephens Posted December 3, 2015 Author Share Posted December 3, 2015 @SiNNuT, thanks! I agree. I just like to have a fallback in case the summary field isn't filled in every time. @kongondo's code actually checks for a summary field first, and my imitation does likewise. 2 Link to comment Share on other sites More sharing options...
SiNNuT Posted December 3, 2015 Share Posted December 3, 2015 @SiNNuT, thanks! I agree. I just like to have a fallback in case the summary field isn't filled in every time. @kongondo's code actually checks for a summary field first, and my imitation does likewise. Ah ok, i did not check the code. Using it as fallback is fine. However, in your OP you mention that you need valid html, so i gathered that you need to truncate with keeping html tags. This is quite tricky but you could use something ready-made like the truncate function of CakePHP: https://github.com/cakephp/cakephp/blob/master/src/Utility/Text.php#L539-L665 http://book.cakephp.org/3.0/en/core-libraries/string.html#truncating-text Link to comment Share on other sites More sharing options...
johnstephens Posted December 3, 2015 Author Share Posted December 3, 2015 …in your OP you mention that you need valid html, so i gathered that you need to truncate with keeping html tags. That's what I'm used to doing with the above-mentioned Textpattern plugin, but @kongondo's suggestion was good enough for the implementation I was working on yesterday. Forgive my limited understanding, but CakePHP is something like Rails for PHP, right? So taking this suggestion would mean imitating the Cake code, not installing Cake? I just want to be sure I'm not imagining something harder than it needs to be. Thanks again! http://book.cakephp.org/3.0/en/core-libraries/string.html#truncating-text This link appears to lead to an nginx 404 error. Link to comment Share on other sites More sharing options...
SiNNuT Posted December 3, 2015 Share Posted December 3, 2015 I think the function i linked to is not dependent on anything else in cakephp so you could just copy paste it in some file and include it in your template so that you can use the function (you can drop the public static part). I can't test this for you but it should work. Link to comment Share on other sites More sharing options...
Macrura Posted December 4, 2015 Share Posted December 4, 2015 how about wordLimiter? i have this in every single site. 2 Link to comment Share on other sites More sharing options...
mr-fan Posted December 4, 2015 Share Posted December 4, 2015 Different set of tools here (but based on somas great wordlimiter! I don't use it in a module via hook just in my _func.php and i use it on several different strings to get clean values for example a sumary of the body text if no meta description is set...and so on. regards mr-fan /** * Wordlimiter cuts a textarea only after complete words not between * used seo function and in some templates */ function wordLimiter($str = '', $limit = 120, $endstr = '...'){ if($str == '') return ''; if(strlen($str) <= $limit) return $str; $out = substr($str, 0, $limit); $pos = strrpos($out, " "); if ($pos>0) { $out = substr($out, 0, $pos); } $out .= $endstr; return $out; } /** * Alternative with regex for striptags function * used for seo function and in some templates */ function ripTags($string) { // ----- remove HTML TAGs ----- $string = preg_replace ('/<[^>]*>/', ' ', $string); // ----- remove control characters ----- $string = str_replace("\r", '', $string); // --- replace with empty space $string = str_replace("\n", ' ', $string); // --- replace with space $string = str_replace("\t", ' ', $string); // --- replace with space // ----- remove multiple spaces ----- $string = trim(preg_replace('/ {2,}/', ' ', $string)); return $string; } 4 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now