Negating specific words with regexes isn't a good practice (see a deep
discussion here:, in your
case I would resolve it like this:

$s = 'cats i saw a cat and a dog';
var_dump(preg_split('/(dog|cat)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE |

That will output:

array(5) {
  string(3) "cat"
  string(10) "s i saw a "
  string(3) "cat"
  string(7) " and a "
  string(3) "dog"

Then you just have to go through the result array of preg_split and
concatenate every "cat" and "dog".



On Sat, Aug 22, 2009 at 12:32 PM, דניאל דנון<> wrote:
> Lets assume I have the string "cats i  saw a cat and a dog"
> i want to strip everything except "cat" and "dog" so the result will be
> "catcatdog",
> using preg_replace.
> I've tried something like /[^(dog|cat)]+/ but no success
> What should I do?
> --
> Use ROT26 for best security

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to