src/ORM/FieldType/DBText.php
public function ContextSummary(
        $characters = 500,
        $keywords = null,
        $highlight = true,
        $prefix = "... ",
        $suffix = "..."
    ) {

        if (!$keywords) {
            // Use the default "Search" request variable (from SearchForm)
            $keywords = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : '';
        }

        // Get raw text value, but XML encode it (as we'll be merging with HTML tags soon)
        $text = nl2br(Convert::raw2xml($this->Plain()));
        $keywords = Convert::raw2xml($keywords);

        // Find the search string
        $position = empty($keywords) ? 0 : (int) mb_stripos($text, $keywords);

        // We want to search string to be in the middle of our block to give it some context
        $position = max(0, $position - ($characters / 2));

        if ($position > 0) {
            // We don't want to start mid-word
            $position = max(
                (int) mb_strrpos(mb_substr($text, 0, $position), ' '),
                (int) mb_strrpos(mb_substr($text, 0, $position), "\n")
            );
        }

        $summary = mb_substr($text, $position, $characters);
        $stringPieces = explode(' ', $keywords);

        if ($highlight) {
            // Add a span around all key words from the search term as well
            if ($stringPieces) {
                foreach ($stringPieces as $stringPiece) {
                    if (mb_strlen($stringPiece) > 2) {
                        // Maintain case of original string
                        $summary = preg_replace(
                            '/' . preg_quote($stringPiece, '/') . '/i',
                            '<mark>$0</mark>',
                            $summary
                        );
                    }
                }
            }
        }
        $summary = trim($summary);

        // Add leading / trailing '...' if trimmed on either end
        if ($position > 0) {
            $summary = $prefix . $summary;
        }
        if (strlen($text) > ($characters + $position)) {
            $summary = $summary . $suffix;
        }

        return $summary;
    }
public function ContextSummary(
        $characters = 500,
        $keywords = null,
        $highlight = true,
        $prefix = "... ",
        $suffix = "..."
    ) {

        if (!$keywords) {
            // Use the default "Search" request variable (from SearchForm)
            $keywords = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : '';
        }

        // Get raw text value, but XML encode it (as we'll be merging with HTML tags soon)
        $text = nl2br(Convert::raw2xml($this->Plain()));
        $keywords = Convert::raw2xml($keywords);

        // Find the search string
        $position = empty($keywords) ? 0 : (int) mb_stripos($text, $keywords);

        // We want to search string to be in the middle of our block to give it some context
        $position = max(0, $position - ($characters / 2));

        if ($position > 0) {
            // We don't want to start mid-word
            $position = max(
                (int) mb_strrpos(mb_substr($text, 0, $position), ' '),
                (int) mb_strrpos(mb_substr($text, 0, $position), "\n")
            );
        }

        $summary = mb_substr($text, $position, $characters);
        $stringPieces = explode(' ', $keywords);

        if ($highlight) {
            // Add a span around all key words from the search term as well
            if ($stringPieces) {
                foreach ($stringPieces as $stringPiece) {
                    if (mb_strlen($stringPiece) > 2) {
                        // Maintain case of original string
                        $summary = preg_replace(
                            '/' . preg_quote($stringPiece, '/') . '/i',
                            '<mark>$0</mark>',
                            $summary
                        );
                    }
                }
            }
        }
        $summary = trim($summary);

        // Add leading / trailing '...' if trimmed on either end
        if ($position > 0) {
            $summary = $prefix . $summary;
        }
        if (strlen($text) > ($characters + $position)) {
            $summary = $summary . $suffix;
        }

        return $summary;
    }
public function ContextSummary ( $ characters = 500 , $ keywords = null , $ highlight = true , $ prefix = "... " , $ suffix = "..." ) { if ( ! $ keywords ) { // Use the default "Search" request variable (from SearchForm) $ keywords = isset ( $ _REQUEST [ 'Search' ] ) ? $ _REQUEST [ 'Search' ] : '' ; } // Get raw text value, but XML encode it (as we'll be merging with HTML tags soon) $ text = nl2br ( Convert :: raw2xml ( $ this -> Plain ( ) ) ) ; $ keywords = Convert :: raw2xml ( $ keywords ) ; // Find the search string $ position = empty ( $ keywords ) ? 0 : ( int ) mb_stripos ( $ text , $ keywords ) ; // We want to search string to be in the middle of our block to give it some context $ position = max ( 0 , $ position - ( $ characters / 2 ) ) ; if ( $ position > 0 ) { // We don't want to start mid-word $ position = max ( ( int ) mb_strrpos ( mb_substr ( $ text , 0 , $ position ) , ' ' ) , ( int ) mb_strrpos ( mb_substr ( $ text , 0 , $ position ) , "\n" ) ) ; } $ summary = mb_substr ( $ text , $ position , $ characters ) ; $ stringPieces = explode ( ' ' , $ keywords ) ; if ( $ highlight ) { // Add a span around all key words from the search term as well if ( $ stringPieces ) { foreach ( $ stringPieces as $ stringPiece ) { if ( mb_strlen ( $ stringPiece ) > 2 ) { // Maintain case of original string $ summary = preg_replace ( '/' . preg_quote ( $ stringPiece , '/' ) . '/i' , '<mark>$0</mark>' , $ summary ) ; } } } } $ summary = trim ( $ summary ) ; // Add leading / trailing '...' if trimmed on either end if ( $ position > 0 ) { $ summary = $ prefix . $ summary ; } if ( strlen ( $ text ) > ( $ characters + $ position ) ) { $ summary = $ summary . $ suffix ; } return $ summary ; }
Perform context searching to give some context to searches, optionally
highlighting the search term.

@param int $characters Number of characters in the summary
@param string $keywords Supplied string ("keywords"). Will fall back to 'Search' querystring arg.
@param bool $highlight Add a highlight <mark> element around search query?
@param string $prefix Prefix text
@param string $suffix Suffix text
@return string HTML string with context
Perform context searching to give some context to searches optionally highlighting the search term .