Search results: 9 words bef. keyword, 9 words after keyword |
[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
[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.
[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 ?
[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.
[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)
[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) Code: <?php 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!
[eluser]Rubiz'[/eluser]
Oh my question is becomming a new CI feature?? //// cool!!!
[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. |
Welcome Guest, Not a member yet? Register Sign In |