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