Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16751 > unrolled thread
| Started by | Steve <StevePBurgess@gmail.com> |
|---|---|
| First post | 2016-05-29 07:41 -0700 |
| Last post | 2016-05-29 18:58 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.php
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
| From | Steve <StevePBurgess@gmail.com> |
|---|---|
| Date | 2016-05-29 07:41 -0700 |
| Subject | Regular 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]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2016-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]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-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