Re: str_replace on words with an array
| From: | Stut | Date: | Mon, 30 Oct 2006 21:18:33 +0000 |
| Subject: | Re: str_replace on words with an array | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-243911@lists.php.net to get a copy of this message | ||
Dotan Cohen wrote:
> Er, so how would it be done? I've been trying for two days now with no
> success.
Ok, I guess my original reply didn't get through, or you ignored it.
Here it is again for your convenience.
Dotan Cohen wrote:
> > $searchQuery=str_replace( "^".$noiseArray."$", " ",
> > $searchQuery);
Ok, this is what the compiler will see...
$searchQuery=str_replace("^Array$", " ", $searchQuery);
Yes, that's a literal Array in the string. You cannot, and you should
remember this, you cannot concatenate strings and arrays. What would you
expect it to do?
Now, the answer is this...
$searchQuery = str_replace($noiseArray, ' ', $searchQuery);
However, what you seem to be doing is putting regex syntax where it
would have no effect even if $noiseArray was not an array. If your
intention is to replace the words rather than just the strings then you
need to look at preg_replace (http://php.net/preg_replace) and you'll
need to decorate the strings in $noiseArray with the appropriate
characters to make them the search pattern you need - full details on
the preg_replace manual page.
-Stut (painfully sober now :( )