Welcome Guest, Not a member yet? Register   Sign In
Search results: 9 words bef. keyword, 9 words after keyword
#11

[eluser]Derek Allard[/eluser]
let me also point out the EllisLab Developer guidelines which will be really handy here.
#12

[eluser]Bramme[/eluser]
[quote author="Derek Allard" date="1212526510"]Thanks Bramme. Good start, but I think there's some room for improvement here. I'd also like to solicit the whole community to give thoughts. Anyone have anything to add? For one, CI already has a highlight function in the text helper, we should probably use it and not a regex. Anyone else? Let's keep em coming.[/quote]
-the highlight boolean could be changed to an integer: 0 = false, 1 = cut off from last space in the string (resulting in lesser characters left and right than boundary) or 2 = keeping the cut off words complete, resulting in more characters left and right than boundary.
- I'm too new to CI to know anything about that highlight thing. I'll check it out.
- Add ... to beginning and end. (maybe optional? Though that would make a lot of variables)
-solution for lots of variables: enter array for the parameters.
- possibility to add first and last $boundary (or another number) characters of $text in the excerpt too.
- change $boundary to select amount of words, not characters.
#13

[eluser]Rubiz'[/eluser]
Well guys, I'm not "mature" enough to give you ideas, I'll be waiting the result happy for help CI to grow on new functions, accidentally Smile
#14

[eluser]Rick Jolly[/eluser]
Edit: Scratch this - it's about 100 times slower than Bramme's. A couple of errors to mention though:
1) Oops, I have mixed up the $text and $phrase parameters.
2) Bramme's version doesn't seem to include enough words.

Here's my attempt, roughly using the method I described above. I didn't include the $highlight parameter because it's easy enough to run the result through "highlight_phrase()" and the parameter didn't describe HOW to highlight. There would need to be 2 parameters for highlight as in "highlight_phrase()".

Code:
function excerpt ($text, $phrase, $boundary)
    {
        $space_before = '';
        $space_after = '';
        $text_length = strlen($text);
        $text_position = stripos($phrase, $text);
        
        if ($text_position === false)
        {
            return false;
        }
        
        // whole substring before $text
        $before = substr($phrase, 0, $text_position);
        if (strlen($before) > 0)
        {
            // The "character_limiter" method strips spaces around words - we need to
            // keep the space before the "$text" if a space exists.
            if (substr($before, strlen($before) -1) === ' ')
            {
                $space_before = ' ';
            }
        }
        
        // whole substring after $text
        $after = substr($phrase, $text_position + $text_length);
        if (strlen($after) > 0)
        {
            // The "character_limiter" method strips spaces around words - we need to
            // keep the space after the "$text" if a space exists.
            if (substr($after, 0, 1) === ' ')
            {
                $space_after = ' ';
            }
        }

        $before = strrev(character_limiter(strrev($before), $boundary, ''));
        $after = character_limiter($after, $boundary, '');

        return $before . $space_before . $text . $space_after . $after;
    }

Examples:
Code:
$phrase = "Lorem ipsum dolor sit amet, consectetur adipisicing elit.";
excerpt('dolor', $phrase, 1); // produces: ipsum dolor sit
excerpt('dol', $phrase, 1); // produces: ipsum dolor
excerpt('dolor', $phrase, 20); // produces: Lorem ipsum dolor sit amet, consectetur
excerpt('dolor', $phrase, 1000); // produces: Lorem ipsum dolor sit amet, consectetur adipisicing elit.
#15

[eluser]Popcorn[/eluser]
Here's my attempt Derek,

Code:
function excerpt ($text, $phrase, $boundary = 10, $words = false)
    {
        if ($text == '' && $phrase == '')
        {
            return false;
        }

        if ($words == true) # Word Boundary
        {
            $array = explode(" ", $text);
            $position = array_search($phrase, $array);
              
            $left = $position - $boundary;
            $right = $position + $boundary;

            $new = '';
            
            for ($i=$left; $i <= $right; $i++) {
                $new .= ' '.$array[$i];
            }
            return $new;
        }
        else # Character Boundary
        {
            $length = strlen($phrase);
            $position = strpos($text, $phrase);
            $start = $position - $boundary;
            return substr($text, $start, $boundary * 2 + $length);
        }
    }

    $text = "the quick red fox jumped over the lazy dog";
    $phrase = "red";

    echo excerpt($text, $phrase, 2, true);
    # Returns "the quick red fox jumped"

    echo excerpt($text, $phrase, 4, false);
    # Returns "ick red fox"

Did a quick benchmark

Word Mode

100 Req : 0.001465
1,000 Req : Total Time : 0.013749
10,000 Req : Total Time : 0.134911




Theme © iAndrew 2016 - Forum software by © MyBB