[PHP] Re: Weird preg issue

2010-11-04 Thread Alex Nikitin
Ah, i seem to have figured out the problem here... if you run a
preg_match_all it will return

[0] = Array
(
[0] = And the cow says
[1] =
[2] = Moo
[3] =
)

And preg_replace is global by default, so in order for this to work
correctly, not sure about the elegantly part, but you can just limit
preg_replace

preg_replace('/(?:(?![a-zA-Z\s]*).)*/', '', $str, 1);

and that seems to work correctly...

Neat... kinda...

On Thu, Nov 4, 2010 at 3:47 PM, Alex Nikitin niks...@gmail.com wrote:

 Hi,

 I'm kind of new to this list, and so if there have been discussions about
 this, i am not quite aware of them (i tried searching), but i ran across
 this issue and i figured it would be interesting enough to show you guys
 here:

 I was looking for a way to replace all the text in a string that doesn't
 match a pattern with nothing (therefore string in, only part of the string
 that matches my pattern out), one line with no arrays in the middle; and i
 guess there is a way to do this with temp variables, well i know there is,
 but i kind of wanted a more elegant solution, so i came up with this match
 line

 $str = 'And the cow says Mooo';
 preg_match('/(?:(?![a-zA-Z\s]*).)*/', $str, $matches);
 print_r($matches);

 output:
 Array
 (
 [0] = And the cow says
 )

 so i was pretty happy to see that, so if i pass that expression to
 preg_replace it should, hopefully, replace that text with nothing, and i
 theoretically should be left with Mooo, which was my goal originally, so i
 run

 print_r(preg_replace('/(?:(?![a-zA-Z\s]*).)*/', '', $str));

 output:
 

 ... Hardly what i was expecting... Any ideas? bug, something i'm not
 getting, something in the way preg works?

 Thanks in advance,



 ~ Alex



[PHP] Re: Weird preg issue

2010-11-04 Thread Alex Nikitin
but that doesnt work if you add something after the Mooo *sigh*.
well it gets kept


On Thu, Nov 4, 2010 at 3:47 PM, Alex Nikitin niks...@gmail.com wrote:

 Hi,

 I'm kind of new to this list, and so if there have been discussions about
 this, i am not quite aware of them (i tried searching), but i ran across
 this issue and i figured it would be interesting enough to show you guys
 here:

 I was looking for a way to replace all the text in a string that doesn't
 match a pattern with nothing (therefore string in, only part of the string
 that matches my pattern out), one line with no arrays in the middle; and i
 guess there is a way to do this with temp variables, well i know there is,
 but i kind of wanted a more elegant solution, so i came up with this match
 line

 $str = 'And the cow says Mooo';
 preg_match('/(?:(?![a-zA-Z\s]*).)*/', $str, $matches);
 print_r($matches);

 output:
 Array
 (
 [0] = And the cow says
 )

 so i was pretty happy to see that, so if i pass that expression to
 preg_replace it should, hopefully, replace that text with nothing, and i
 theoretically should be left with Mooo, which was my goal originally, so i
 run

 print_r(preg_replace('/(?:(?![a-zA-Z\s]*).)*/', '', $str));

 output:
 

 ... Hardly what i was expecting... Any ideas? bug, something i'm not
 getting, something in the way preg works?

 Thanks in advance,



 ~ Alex