Re: [PHP] str_replace on words with an array



Dotan Cohen wrote:
I need to remove the noise words from a search string. I can't seem to
get str_replace to go through the array and remove the words, and I'd
rather avoid a redundant foreach it I can. According to TFM
str_replace should automatically go through the whole array, no? Does
anybody see anything wrong with this code:

$noiseArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"\"", "'", ":", ";", "|", "\\", "<", ">", ",", ".", "?", "$", "!",
"@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "[",
"]", "{", "}", "about", "after", "all", "also", "an", "and",
"another", "any", "are", "as", "at", "be", "because", "been",
"before", "being", "between", "both", "but", "by", "came", "can",
"come", "could", "did", "do", "does", "each", "else", "for", "from",
"get", "got", "has", "had", "he", "have", "her", "here", "him",
"himself", "his", "how", "if", "in", "into", "is", "it", "its",
"just", "like", "make", "many", "me", "might", "more", "most", "much",
"must", "my", "never", "now", "of", "on", "only", "or", "other",
"our", "out", "over", "re", "said", "same", "see", "should", "since",
"so", "some", "still", "such", "take", "than", "that", "the", "their",
"them", "then", "there", "these", "they", "this", "those", "through",
"to", "too", "under", "up", "use", "very", "want", "was", "way", "we",
"well", "were", "what", "when", "where", "which", "while", "who",
"will", "with", "would", "you", "your", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z");

$searchQuery=str_replace( "^".$noiseArray."$", " ", $searchQuery);

it's string replacement not regexp replacement, therefore the '^' and '$' are bogus.
the first argument to the str_replace() call is the string literal:

'^Array$'

.... you can't concatenate strings and arrays like that (and get the result you want)!

instead, your function call should look like this:

$searchQuery = str_replace($noiseArray, ' ', $searchQuery);


Thanks in advance.

Dotan Cohen

http://essentialinux.com
http://what-is-what.com/what_is/sitepoint.html

.



Relevant Pages

  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • RE: Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)