Re: str_replace on words with an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 29/10/06, Børge Holen <borge@xxxxxxxxxxx> wrote:> Yes you need to put some \ in front of some of those characters>> On Sunday 29 October 2006 21:05, 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);> >> > Thanks in advance.> >
I improved the $noiseArray to this:
$noiseArray = array("[:alnum:]", "[:punct:]", "|", "\\", "<", ">","#", "@",  "\$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=","[", "]", "{", "}", "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");
Do any of the characters in there need escaping (other than the $which is already escaped)?
How does on go about looping through the array, matching only wholewords? This didn't quite do it:$searchQuery=str_replace( "^".$noiseArray."$", " ", $searchQuery);
And neither did this:$searchQuery=str_replace( "/^".$noiseArray."$/", " ", $searchQuery);
Thanks.
Dotan Cohenhttp://dotancohen.comhttp://what-is-what.com/what_is/drm.html

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux