We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37131
    • 111 Posts
    There is no introtext in these posts, content of post is inrested and this is ok.
      ---
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Martin Golding
    • For reference, here's the summarization/truncation code I use. I typically create it as a snippet called 'trunc' and use it like [ [*content:trunc]]:

      <?php
      if (!isset($options)) $options = 1000;
      $charcount = $options;
      
      $output = $input;
      $pad = " ...";
      
      if(false !== ($breakpoint = strpos($output, " ", $charcount))) { 
      	if($breakpoint < strlen($output) - 1) { 
      		$output = substr($output, 0, $breakpoint) . $pad; 
      	} 
      } 
      
      $opened = array();
      
      if(preg_match_all("/<(\/?[a-z]+)>?/i", $output, $matches)) { 
      	foreach($matches[1] as $tag) { 
      		if(preg_match("/^[a-z]+$/i", $tag, $regs)) { 
      			if(strtolower($regs[0]) != 'br' || strtolower($regs[0]) != 'hr') $opened[] = $regs[0]; 
      		} elseif(preg_match("/^\/([a-z]+)$/i", $tag, $regs)) { 
      			unset($opened[array_pop(array_keys($opened, $regs[1]))]); 
      		} 
      	} 
      }  
      if($opened) { 
      	$tagstoclose = array_reverse($opened); 
      	foreach($tagstoclose as $tag) $output .= "</$tag>"; 
      }
      
      
      return $output;
      [ed. note: rethrash last edited this post 12 years, 2 months ago.]
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 37131
        • 111 Posts
        Ok, I've finally understood what BobRay said in second post smiley)
        Ellipsis can cut content at it's limit and this leaves unclosed tags, or worse.Final effect can be controlled only by using written introtext or whole posts, but blog with just daily text post looks quite badand with whole posts can be quite big and messy.

        Is there any way I could make a blog page with pictures, etc without showing whole post in every row ?

        PS. One quick solution that comes to my mind is inserting special tag into post text, kind of IntroEndsHere, but it should be supported at least by ellipsis.
        Well, no, it's stupid. It would work for IT people but not for everyone. undecided [ed. note: khyrlik last edited this post 12 years, 2 months ago.]
          ---
          Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Martin Golding
          • 37131
          • 111 Posts
          Well, I see I should read thoroughly what people post before writting anything wink
          First of all, some 'problems' and code I have posted was copied from Firebug console, which was messing things with these unclosed tags. Sorry, my bad.
          Now, with such great code Ryan posted I could move ahead but still couldn't stand one thing, which was messing my site. What if it will cut output in the middle of tag definition (for example long IMG or A tags) ? So I've fixed it:
          ?php
          if (!isset($options)) $options = 1000;
          $charcount = $options;
           
          $output = $input;
          $pad = " .....";
          $addpad = false;
           
          if(false !== ($breakpoint = strpos($output, " ", $charcount))) {
            if ($breakpoint < strlen($output) - 1) {
              $output = substr($output, 0, $breakpoint); 
              $addpad = true;
            }
          }
           
          $opened = array();
           
          if(preg_match_all("/<(\/?[a-z]+)>/i", $output, $matches)) {
              foreach($matches[1] as $tag) {
                  if(preg_match("/^[a-z]+$/i", $tag, $regs)) {
                      if(strtolower($regs[0]) != 'br' || strtolower($regs[0]) != 'hr') $opened[] = $regs[0];
                  } elseif(preg_match("/^\/([a-z]+)$/i", $tag, $regs)) {
                      unset($opened[array_pop(array_keys($opened, $regs[1]))]);
                  }
              }
          } 
          
          /* checking for unfinished tag at the end */
          $loab = strrpos($output, "<");
          if (!$loab) {$loab = -1;}
          $lcab = strrpos($output, ">");
          if (!$lcab) {$lcab = -1;}
          /*if we found that there is unfinished tag, let's trim output, removing this tag */
          if ($loab > $lcab) {$output = substr($output, 0, $loab);}
          /* now we can add pad */
          if ($addpad) {$output .= $pad;}
          /* the rest - closing opened tags */
          if($opened) {
              $tagstoclose = array_reverse($opened);
              foreach($tagstoclose as $tag) $output .= "</$tag>";
          }
           return $output;

          It works well on my site and I feel like damn god of coding now smiley. I have not programmed anything for years. Because of this and my zero experience with regular expressions this code may be non-godlike wink.
          Thanks everyone for helping !
            ---
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Martin Golding
            • 3749
            • 24,544 Posts
            Congratulations. It looks very good.

            FYI, I just noticed that the next release of MODX has a fix for the ellipsis modifier to keep it from breaking tags. wink

            You can compare your code to the official version.


            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODX info for everyone: http://bobsguides.com/modx.html
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting