Re: str_replace on words with an array

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

 



At 10/29/2006 01:07 PM, Dotan Cohen wrote:
The purpose of the ^ and the $ is to define the beginning and the end of a word:
http://il2.php.net/regex


No, actually, ^ and $ define the beginnning & end of the entire expression being searched, not the boundaries of a single word. Therefore searching for ^mouse$ will locate "mouse" only if it's the only word in the entire string, which I gather is not what you want.

I suspect what you want is either this:

        (^| )WORD( |$)

(the word bounded by either the start/end of string or a space)

or perhaps better yet this:

        \bWORD\b

(the word bounded by word boundaries).

See [PCRE regex] Pattern Syntax
http://il2.php.net/manual/en/reference.pcre.pattern.syntax.php



Further to your problem, I believe this is incorrect:

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

Since $noiseArray is your entire array, it doesn't make sense to enclose it in word boundaries of any kind. Instead, I imagine each member of the array needs to be bounded individually.

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.

Regards,
Paul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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