WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Contextual Related Posts] Suggestion: Limit characters in content that is compared

[Plugin: Contextual Related Posts] Suggestion: Limit characters in content that is compared

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Ajay
    Participant

    @ajay

    I think I can add this function. I understand you mean search within the first X words of the current post when fetching the results?

    stevemagruder
    Member

    @stevemagruder

    That would work too. Although a character limit can work as well if you do something like the following:

    function max_formatted_content($Content, $MaxLength = -1) {
      $TheContent = str_replace("\r", "", str_replace("\n", " "), $TheText));  // Remove CRLFs, leaving space in their wake
    
      if (($MaxLength > 0) && (strlen($Content) > $MaxLength)) {
        $aWords = preg_split("/[\s]+/", substr($Content, 0, $MaxLength));
    
        // Break back down into a string of words, but drop the last one if it's chopped off
        if (substr($Content, $MaxLength, 1) == " ") {
          $TheContent = implode(" ", $aWords);
        }
        else {
          $TheContent = implode(" ", array_slice($aWords, 0, -1));
        }
    
        $TheContent = close_open_tags($TheContent);
      }
    
      return $TheContent;
    }

    Here’s the close_open_tags function it calls:

    function close_open_tags($html, $ignore=array('img', 'hr', 'br')) {
      if (preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $opentags)) {
        $opentags[1] = array_diff($opentags[1], $ignore);
        $opentags[1] = array_values($opentags[1]);
        preg_match_all("#</([a-z]+)>#iU", $html, $closetags);
        $opened = count($opentags[1]);
        if (count($closetags[1]) == $opened) return $html;
        $opentags[1] = array_reverse($opentags[1]);
        for ($i=0;$i<$opened;$i++) {
          if (!in_array($opentags[1][$i], $closetags)) $html .= '</'.$opentags[1][$i].'>';
          else unset($closetags[array_search($opentags[1][$i], $closetags)]);
        }
      }
    
      return $html;
    }
    Plugin Author Ajay
    Participant

    @ajay

    Thanks for the functions steve

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Contextual Related Posts] Suggestion: Limit characters in content that is compared’ is closed to new replies.