preg_replace anything that isn"t WORD
preg_replace anything that isn"t WORD
am 22.08.2009 17:32:23 von daniel danon
--001636ed6968f450db0471bcb09f
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
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
--001636ed6968f450db0471bcb09f--
Re: preg_replace anything that isn"t WORD
am 22.08.2009 18:17:04 von Jonathan Tapicer
Negating specific words with regexes isn't a good practice (see a deep
discussion here: http://www.perlmonks.org/?node_id=3D588315), in your
case I would resolve it like this:
$s =3D 'cats i saw a cat and a dog';
var_dump(preg_split('/(dog|cat)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE |
PREG_SPLIT_NO_EMPTY));
?>
That will output:
array(5) {
[0]=3D>
string(3) "cat"
[1]=3D>
string(10) "s i saw a "
[2]=3D>
string(3) "cat"
[3]=3D>
string(7) " and a "
[4]=3D>
string(3) "dog"
}
Then you just have to go through the result array of preg_split and
concatenate every "cat" and "dog".
Regards,
Jonathan
On Sat, Aug 22, 2009 at 12:32 PM, ×× ××× ×=D7=
××=9F 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 (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: preg_replace anything that isn"t WORD
am 22.08.2009 21:30:05 von 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
Re: preg_replace anything that isn"t WORD
am 22.08.2009 23:37:35 von 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
Re: preg_replace anything that isn"t WORD
am 24.08.2009 15:32:06 von TedD
>On Sat, Aug 22, 2009 at 12:32 PM, ÈýÏÝÂÔ
l@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=20
spaces and then walking the array looking for cat=20
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
Re: preg_replace anything that isn"t WORD
am 24.08.2009 22:39:58 von hack988 hack988
--001636ed6c42aed5960471e9384b
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
Use preg_replace_callback instead!
preg_replace_callback is better performance than preg_replace with /e.
------------------------------------------------------------ ---------------=
------
code
$str=3D"cats i saw a cat and a dog";
$str1=3Dpreg_replace_callback("/(dog|cat|.)/is","call_replac e",$str);
echo $str."
";
echo $str1;
function call_replace($match){
if(in_array($match[0],array('cat','dog')))
return $match[0];
else
return "";
}
2009/8/24 tedd :
>> On Sat, Aug 22, 2009 at 12:32 PM, ÈýÏÝÂÔ
niel@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 b=
e
>>> "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
>
>
--001636ed6c42aed5960471e9384b--
Re: preg_replace anything that isn"t WORD
am 25.08.2009 05:41:57 von Shawn McKenzie
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."
";
> echo $str1;
> function call_replace($match){
> if(in_array($match[0],array('cat','dog')))
> return $match[0];
> else
> return "";
> }
>
> 2009/8/24 tedd :
>>> 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?
>> 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: preg_replace anything that isn"t WORD
am 25.08.2009 05:45:13 von Jonathan Tapicer
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 wrot=
e:
> hack988 hack988 wrote:
>> =A0Use preg_replace_callback instead!
>> preg_replace_callback is better performance than preg_replace with /e.
>> ------------------------------------------------------------ ------------=
---------
>> code
>>
>> $str=3D"cats i =A0saw a cat and a dog";
>> $str1=3Dpreg_replace_callback("/(dog|cat|.)/is","call_replac e",$str);
>> echo $str."
";
>> echo $str1;
>> function call_replace($match){
>> =A0if(in_array($match[0],array('cat','dog')))
>> =A0 return $match[0];
>> =A0else
>> =A0 return "";
>> }
>>
>> 2009/8/24 tedd :
>>>> On Sat, Aug 22, 2009 at 12:32 PM, ÈýÏÝÂÔ
daniel@gmail.com>
>> wrote:
>>>>> =A0Lets assume I have the string "cats i =A0saw a cat and a dog"
>>>>> =A0i want to strip everything except "cat" and "dog" so the result wi=
ll be
>>>>> =A0"catcatdog",
>>>>> =A0using preg_replace.
>>>>>
>>>>>
>>>>> =A0I've tried something like /[^(dog|cat)]+/ but no success
>>>>>
>>>> =A0> 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 th=
e
>>> array looking for cat and dog while assembling the resultant string?
>>>
>>> Cheers,
>>>
>>> tedd
>>>
>>>
>>> --
>>> -------
>>> http://sperling.com =A0http://ancientstones.com =A0http://earthstones.c=
om
>>>
>>> --
>>> 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. =A0Maybe because I use this list as a newsgroup? =A0Anyway, wh=
at
> I posted before:
>
> Match everything but only replace the backreference for the words:
>
>
> $s =3D "cats i =A0saw a cat and a dog";
> $r =3D 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