Reputation: 1305
I am constructing a search function for our CMS, essentially just creating a cfquery that looks in our database for the given text. When I display the results to the browser I was hoping to have as part of the results a section of the text appear that contains the searched for text as well as like a sentence or two surrounding it. So it would be something like blah blah blah #FORM.searchedstring# blah blah blah. Any ideas on how to only display that specific part of the string?
Thanks
This is what I've written
<cfquery datasource="#MyDatasource#" name="getstory">
SELECT *
FROM Stories
WHERE MYSTORIES.TEXT
LIKE '%#FORM.searchstring#%'
OR MYSTORIES.PAGETITLE
LIKE '%#FORM.searchstring#%'
OR MYSTORIES.HEADLINE
LIKE '%#FORM.searchstring#%'
OR MYSTORIES.SUBTITLE
LIKE '%#FORM.searchstring#%'
ORDER BY MNLSECTIONID
</cfquery>
In the body is a cfloop query that returns all entries. I just want to also return a particular part of the sting in MYSTORIES.TEXT that has #FORM.searchstring# in the middle.
Upvotes: 4
Views: 273
Reputation: 7519
Another option, and one that may yield more accurate search results, is to put your data into a search collection (either Solr of Verity depending on which version of CF you are running) using CFINDEX and then use CFSEARCH to search the collection. You can set index to return instances of the search term in the data and even easily add code to highlight said instances.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d04.html
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d02.htm
Upvotes: 1
Reputation: 14333
You could use a FindNoCase()
function to find the position in the text of your #form.searchingstring#
Something like
<cfset locationoftext = findnocase(form.searchstring,getstory.story)>
<cfdump var="#Mid(getstory.story,locationoftext-40,40)#"><Br>
<cfdump var="#Mid(getstory.story,locationoftext,40)#">
Not sure if this is exactly what you're after though
Upvotes: 4