Question

I want to ask about how to deleting content inside quotes that followed by bracket?

For example, I have a string :

$text = 'I have a dog. My dog is cute. "I have a cat". My cat is lazy. "I have a bird" (I said). Come to my house! "You sure?" and I said "Yes, of course" (2009)';

I want result like this:

I have a dog. My dog is cure. "I have a cat". My cat is lazy. Come to my house! "You sure?" and I said.

I already create this regex:

$return_value = preg_replace('/ "[^"]+"+ \([^\(]+\) /', ' ', $text);

But the result just cut at beginning sentences, not all sentences.

Anyone know to solve this problem?

Était-ce utile?

La solution

With input:

I have a dog. My dog is cute. "I have a cat". My cat is lazy. "I have a bird" (I said). Come to my house! "You sure?" and I said "Yes, of course" (2009).

And using:

/ "[^"]+" \([^()]+\)(?:\.(?!\s*$))?/

You get:

I have a dog. My dog is cute. "I have a cat". My cat is lazy. Come to my house! "You sure?" and I said.

You had a whitespace after the \) in your regex which prevented a match. I used (?:\.(?!\s*$))? which means match a dot only if it's not at the end of the string.

regex101 demo

Autres conseils

Try:

echo preg_replace('/"[^"]*"\s*\([^)]*\)\.?/', '', $text);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top