Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.php Subject: Re: Regular Express - preg_replace - matching whole phrases Date: Sun, 29 May 2016 18:58:47 +0100 Organization: A noiseless patient Spider Lines: 35 Message-ID: <87inxwkap4.fsf@bsb.me.uk> References: <231f56c0-320c-4654-aa35-a26bdbac4834@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="4925"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/M0PgQOyT0ekIcIRVij5aa0fMkRlbMeJY=" Cancel-Lock: sha1:jKIitQYyiLp16sMX0yz/6wGMANY= sha1:VS4z7wAuRmqc6KV5PeSDS5AWxB0= X-BSB-Auth: 1.da5999e52c875bb2e15c.20160529185847BST.87inxwkap4.fsf@bsb.me.uk Xref: csiph.com comp.lang.php:16753 Steve writes: > I am doing some replacements on text using arrays e.g. > > $text="Everyone loves apple and banana smoothies"; > $searcharray=array("/\bBanana\b/i","/\bApple\b/i","/\bTomatoes\b/i"); > $replacearray=array("Toffee","Soup","Breadsticks"); > > $newtext=preg_replace($searcharray,$replacearray,$text); > > > I'm using \b so that only full words are replaced and \i to make the > match case insensitive. (You mean /i) > It all works fine - but when I want to search for, say... > > Pineapple... > > i.e. a word with punctuation after it, it doesn't work. > > I've tried escaping the .s to no avail... Maybe you left the \b in there? Maybe you did not escape the escape character correctly? Much better to show what did not work. > Any ideas? "\bBanana\\.\\.\\." works for me. -- Ben.