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

[eluser]Rubiz'[/eluser]
Hi!

My doubt is about logic and not only CI.
I have a news web site, and a search inside it that is already ok; but my client wants that the view layout foreach row of the search results contains: 9 words before de keyword, the keyword in bold (its ready), and 9 words after keyword...

Anyone could gimme some "light" about it before I say "NO!" to the client?

Thanx Smile
#2

[eluser]sikkle[/eluser]
yea yea start there :

http://ca.php.net/manual/en/function.stripos.php


go on the section : See Also..

you will have to play with those a bit and make it happend.

see ya.
#3

[eluser]Rubiz'[/eluser]
thanx sikkle!
#4

[eluser]Rubiz'[/eluser]
Well, I gave a look inthe php manual, in all those functions, and yes it finds the string but... no idea about how organize the script to take prev 50 chars and last 50 chars...

Back to zero... Anyone ?
#5

[eluser]Rick Jolly[/eluser]
If you're just looking for the 50 chars either side, then just use substr in combination with stripos using a little arithmetic.

Edit:
If you want 9 words either side then use stripos and substr to split the phase in two and send both parts through the CI word_limiter() function of the text helper.

Edit again:
For the first 9 words, you might want to use strrev() before word_limiter() and then strrev() again.
#6

[eluser]Rubiz'[/eluser]
wow how many functions hehe, I'll try that!!
#7

[eluser]Derek Allard[/eluser]
If someone wanted to create this function for the text helper, I'd consider adding it to CI core.

Code:
function excerpt ($text, $phrase, $boundary, $highlight)
{

// function will take $text, search through it for $phrase, (and hightlight it if $highlight is TRUE)
// and return $boundary characters on both sides of it.

}
#8

[eluser]Bramme[/eluser]
[quote author="Derek Allard" date="1212461070"]If someone wanted to create this function for the text helper, I'd consider adding it to CI core.

Code:
function excerpt ($text, $phrase, $boundary, $highlight)
{

// function will take $text, search through it for $phrase, (and hightlight it if $highlight is TRUE)
// and return $boundary characters on both sides of it.

}
[/quote]working on it. I'll get back in a bit with my function.

Code:
<?php
function excerpt ($text, $phrase, $boundary = 50, $fullword = true, $highlight = false)
{

// function will take $text, search through it for $phrase, won't cut off full words (if true) and hightlight $phrase if $highlight is TRUE
// and return $boundary characters on both sides of it.
    $phrasepos = strpos($text, $phrase);
    if($phrasepos !== false)
    {
        $left = $phrasepos < $boundary ? 0 : $phrasepos - $boundary;
        $length = $boundary * 2 + strlen($phrase);
        
        $temp_excerpt = substr($text, $left, $length);
        
        
        if($fullword)
        {
            $excerpt = substr($temp_excerpt, 0, strrpos($temp_excerpt, ' '));
            $excerpt = substr($excerpt, strpos($excerpt, ' ') + 1);
        } else
        {
            $excerpt = $temp_excerpt;
        }
        
        if($highlight)
        {
            $excerpt = ereg_replace($phrase, '<b>'.$phrase.'</b>', $excerpt);
        }
        
        return $excerpt;
    } else
    {
        return "Phrase not found in text";
    }

}

$text = "Hello, my name is Bram Van der Sype and for you today, I wrote this little piece of code. It's probably not perfect, but it works! At least, that's what I hope!";
$phrase = "code";
echo excerpt($text, $phrase), "<br />\n";
// returns "and for you today, I wrote this little piece of code. It's probably not perfect, but it works! At"
echo excerpt($text, $phrase, 50, false, true), "<br />\n";
// returns "e and for you today, I wrote this little piece of <b>code</b>. It's probably not perfect, but it works! At leas"
?&gt;

Not fully tested, could have some quirks in it, but does the trick. I added an extra variable to the function: fullword. I'm guessing you'll always want this, but you never know...
The string returned also isn't twice the boundary, but twice the boundary + the length of the searched phrase. Seemed only fair to me!
#9

[eluser]Rubiz'[/eluser]
Oh my question is becomming a new CI feature??
//// cool!!!
#10

[eluser]Derek Allard[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB