Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16751 > unrolled thread

Regular Express - preg_replace - matching whole phrases

Started bySteve <StevePBurgess@gmail.com>
First post2016-05-29 07:41 -0700
Last post2016-05-29 18:58 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Regular Express - preg_replace - matching whole phrases Steve <StevePBurgess@gmail.com> - 2016-05-29 07:41 -0700
    Re: Regular Express - preg_replace - matching whole phrases Luuk <luuk@invalid.lan> - 2016-05-29 17:25 +0200
    Re: Regular Express - preg_replace - matching whole phrases Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-29 18:58 +0100

#16751 — Regular Express - preg_replace - matching whole phrases

FromSteve <StevePBurgess@gmail.com>
Date2016-05-29 07:41 -0700
SubjectRegular Express - preg_replace - matching whole phrases
Message-ID<231f56c0-320c-4654-aa35-a26bdbac4834@googlegroups.com>
Hi 

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.

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...

Any ideas?

Many thanks
Steve

[toc] | [next] | [standalone]


#16752

FromLuuk <luuk@invalid.lan>
Date2016-05-29 17:25 +0200
Message-ID<574b09d7$0$5848$e4fe514c@news.xs4all.nl>
In reply to#16751
On 29-05-16 16:41, Steve wrote:
> Hi
>
> 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.
>
> 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...
>
> Any ideas?
>
> Many thanks
> Steve
>

add a 'u'
$text="banana's";
$searcharray=array("/\bBanana.s\b/ui");
$replacearray=array("Toffee");
$newtext=preg_replace($searcharray,$replacearray,$text);
print $newtext;

http://stackoverflow.com/questions/3426265/php-string-replace-match-whole-word
http://php.net/manual/en/reference.pcre.pattern.modifiers.php


[toc] | [prev] | [next] | [standalone]


#16753

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-05-29 18:58 +0100
Message-ID<87inxwkap4.fsf@bsb.me.uk>
In reply to#16751
Steve <StevePBurgess@gmail.com> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web