Removing Words from string?

Discussion in 'Programming' started by amaze, Aug 30, 2007.

  1. #1
    Hi,

    If I have the following list:

    <cfset deadWords = "tabs,tablets,caps,capsules,powder">
    Code (markup):
    ...and I have have a simple string.

    #searchQuery#
    Code (markup):
    What is the best and most efficent practise to remove ANY of the strings in the list? So after the process #searchQuery# shouldn't contain any of the words in #deadWords#.

    Thanks :)
     
    amaze, Aug 30, 2007 IP
  2. amaze

    amaze Active Member

    Messages:
    594
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Came up with a solution for anyone interested :)
    
    	<!--- dead words --->
    	<cfset deadWords = "the,it,and,now,when,how,do">
    	
    	<!--- limit to 50 chars --->
    	<cfset searchQuery 		= left(ARGUMENTS.search,50)>	
    	<!--- put words in array --->
    	<cfset words		 	= listtoarray(searchQuery," ")>
    	<!--- reset --->
    	<cfset searchQuery 		= "">		
    	
    			<cfoutput>
    	<!--- --->
    	<cfloop index="idx" from="1" to="#arrayLen(words)#">
    		<!--- any dead words? --->
    		<cfif NOT listFindNoCase(deadWords, words[idx])>
    			<cfset searchQuery = searchQuery & words[idx]>
    		</cfif>
    	</cfloop>
    PHP:
     
    amaze, Aug 31, 2007 IP
  3. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    seems to me you could do the following..

    
    <cfset deadwords = "tabs,tablets,caps,capsules,powder">
    <cfset searchQuery= replacelist(searchQuery,deadwords,"")>
    
    Code (markup):

    i'm not entirely sure on how fast the replacelist function works.. but this seems to cut down on code pretty well
     
    Jamie18, Aug 31, 2007 IP
  4. amaze

    amaze Active Member

    Messages:
    594
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Yes looks better to me! :)
     
    amaze, Aug 31, 2007 IP
  5. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    I'm glad we had this discussion! :)
     
    datropics, Sep 1, 2007 IP