Forum Moderators: coopster
Later, if things get hairy and you have some of your search terms inside of tags, you may want to use a fancier replacement with preg_replace. However, it's usually better to start out simple and learn as you go.
Code not tested; try before you buy.
this is possible by doing something like a bruteforce - but that wouldn't be elegant - and would need me to get a faster server....
OR
has anybody got an other aproach of generating snippets? (this is not highlighting but generating aprox. 3 to 4 lines of a preview of the whole page with highlighted search terms - I know how to do the highlighting but I don't know how to get the text form the whole page down to a couple of lines)
- save all the positions of all search terms within the page in an array.
Notes on this algo: It's not entirely accurate - won't always maximize. In the step where you find how many other search terms are x away from each search term, this of course will include search terms x before your search term, and x after your search term, when in reality if your search term were precisely in the middle, it would be more like x/2. However, it won't be a bad 'guesstimate'. You could refine this step; but it'd make your code more complicated and slower. Or you could just tweak with the x value at this point and use 3x/4 or x/2.