Re: str_replace on words with an array

From: Date: Sun, 29 Oct 2006 22:31:21 +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-243847@lists.php.net to get a copy of this message
Paul Novitski wrote:
If you go this route, perhaps you could enclose each member of your original array in \b word boundary sequences using an array_walk routine so that you don't have to muddy your original array declaration statement.
At 10/29/2006 01:54 PM, rich gray wrote:
IIRC str_replace() does not interpret or understand regular expression syntax - you'd need preg_replace() for that
You're absolutely right -- I was focusing so much on the regexp syntax that I failed to broaden my gaze... When the OP corrects his PHP to use preg_replace() instead of str_replace(), I believe he'll still need to provide word boundaries around each member of his noise-word array, otherwise the function will simply remove all letters and digits from the words in the search-string he considers meaningful and he'll end up searching thin air. Aside, without knowing the context of his search, it seems a bit extreme to remove all single characters from the search string. It's not hard to think of examples of them occurring as part of valid entity names in our universe -- she got an A, Plan B From Outer Space, Vitamin C, etc. An alternative strategy might be to trust the user to enter a valid search string and simply warn them that the quality of the answer they get will depend on the quality of their input. Regards, Paul

« previous php.general (#243847) next »