Re: preg_replace anything that isn't WORD
| From: | Jonathan Tapicer | Date: | Tue, 25 Aug 2009 03:45:13 +0000 |
| Subject: | Re: preg_replace anything that isn't WORD | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297165@lists.php.net to get a copy of this message | ||
For the record Shawn: I received your previous post from Aug 22 and I
think that it is the best solution.
Jonathan
On Tue, Aug 25, 2009 at 12:41 AM, Shawn McKenzie<nospam@mckenzies.net> wrote:
> hack988 hack988 wrote:
>> Use preg_replace_callback instead!
>> preg_replace_callback is better performance than preg_replace with /e.
>> ---------------------------------------------------------------------------------
>> code
>>
>> $str="cats i saw a cat and a dog";
>> $str1=preg_replace_callback("/(dog|cat|.)/is","call_replace",$str);
>> echo $str."<BR/>";
>> echo $str1;
>> function call_replace($match){
>> if(in_array($match[0],array('cat','dog')))
>> return $match[0];
>> else
>> return "";
>> }
>>
>> 2009/8/24 tedd <tedd.sperling@gmail.com>:
>>>> On Sat, Aug 22, 2009 at 12:32 PM,
>>>> “•ÈýÏÝ“•ÂÔ<danondaniel@gmail.com>
>> 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?
>>> Lot's of ways to skin this cat/dog.
>>>
>>> What's wrong with exploding the string using spaces and then walking the
>>> array looking for cat and dog while assembling the resultant string?
>>>
>>> Cheers,
>>>
>>> tedd
>>>
>>>
>>> --
>>> -------
>>> http://sperling.com
>>> http://ancientstones.com
>>> http://earthstones.com
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
> Certain posts of mine seem to get sucked into a black hole and I never
> see theme. Maybe because I use this list as a newsgroup? Anyway, what
> I posted before:
>
> Match everything but only replace the backreference for the words:
>
>
> $s = "cats i saw a cat and a dog";
> $r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>