benmmc
benmmc

Reputation: 21

How do I remove an entire sentence that includes certain text?

I have this so far:

preg_replace("([^\.\?\!]NNNNNNNNNN)[\.\?\!]","",$string);

and I also tried

preg_replace("([^\.\?\!]\N\N\N\N\N\N\N\N\N\N)[\.\?\!]","",$string);

But it's removing everything in $string.

The text I need to find (and remove the containing sentence) is: "NNNNNNNNNN"

Ideas?

Upvotes: 0

Views: 251

Answers (1)

gpojd
gpojd

Reputation: 23065

What about something like this:

preg_replace("/(^|[.?!])[^.?!]*N{10}[^.?!]*[.?!]/g", "$1", $string);

I'll break it down, but I don't know if it will work.

  • (^|[.?!]) - the end of the previous sentence - captured
  • [^.?!]* - possible multiple things that are not a end of sentence indicator
  • N{10} - what you want to match
  • [^.?!]* - possible multiple things that are not a end of sentence indicator
  • [.?!] - the end of your sentence

Results in PHP:

user@domain:~$ php -a Interactive shell

php > $string = "This is a test. This is NNNNNNNNNN. This is still a test! Or is it?";

php > print preg_replace("/(^|[.?!])[^.?!]*N{10}[^.?!]*[.?!]/", "$1", $string);

This is a test. This is still a test! Or is it?

Upvotes: 4

Related Questions