[PHP] Re: preg_replace anything that isn't WORD

2009-08-22 Thread Shawn McKenzie
Didn't seem to make it the first time.

Shawn McKenzie wrote:
> דניאל דנון 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?
>>

Capture everything but only replace the backreference for the words:

$r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);


-Shawn

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: preg_replace anything that isn't WORD

2009-08-22 Thread Shawn McKenzie
דניאל דנון 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?
> 

Capture everything but only replace the backreference for the words:

$r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php