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);

// another idea based on further reading of the thread:

function pregify($val) {
foreach ((array)$val $k as $v)
$val[$k] = '\b'.preg_quote($v).'\b';

return $val;
}

$searchQuery = preg_replace(pregify($noiseArray), " ", $searchQuery);


Thanks in advance.

Dotan Cohen

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

.



Relevant Pages

  • Re: Creating an Object that extends Array functionality
    ... The goal is not in the OP, just the requirement to avoid modifying ... understanding why augmenting can make some troubles and in which cases ... But for-in loops is also not so useful as for-length ... And in here the only reason to use for-in for array - is ...
    (comp.lang.javascript)
  • Re: getElementById returns null
    ... Don't mix Array() and Array initializers. ... Avoid repeated property access; ... know where the automatic ones happen -- that avoids this comb-like posting ... avoid posting using Google Groups if you ...
    (comp.lang.javascript)
  • Re: java method returning an array of 2 integers
    ... Possible solutions include splitting the method into two, ... > return an array with length 1? ... In stead of saying "it's better in general to avoid this," I would ... the nature of the thing (0th represents width, 1st represents height, ...
    (comp.lang.java.help)
  • Re: getElementById returns null
    ... Don't mix Array() and Array initializers. ... Avoid repeated property access; ... Don't use spacer elements, use CSS margins/paddings instead. ... know where the automatic ones happen -- that avoids this comb-like posting ...
    (comp.lang.javascript)
  • Re: CPU and Memory performance
    ... If you program really needs to shuffle a lot of data, high CPU usage could be normal. ... Don't constantly resize large arrays. ... Place small wait statements in all toplevel loops. ... Avoid overlapping objects on the front panel. ...
    (comp.lang.labview)