Walter Kerelitsch wrote:
hi folks.
I'm in trouble
PHP on apache:
$such =array('/a*d/');
$ersetz=array(' ersetz ');
echo "before!!!! $filestream"; // --
abcdefghi abcdefghi
$filestream=preg_replace($such, $ersetz, $filestream);
echo "after !!!! $filestream"; // --abc
ersetz efghi abc ersetz efghi
but I'm expecting:
ersetzefghi ersetzefghi
where's my error?
tia
walter
Hello Walter,
The asterisk (*), in regular expressions, is not the wildcard character you are
trying to use. It indicates that the preceding character can be repeated any
number of times (even 0 times).
So /a*d/ will match these: "d", "ad", "aad", "aaad", etc. In your case, it
matched and replaced only the "d", leading to what you see.
What you are trying to do requires the regular expression /a.*?d/. The period
(.) matches any single character, like "abd" or "acd". ".*" means any number of
unspecified characters, like "abcd" or "abcdefghi abcd".
The question mark means that it should try to match as few characters as
possible ("non-greedy" search). /a.*d/ may replace "abcdefghi abcd" all at once,
which you don't want - /a.*?d/ makes sure that the matching stops at the first
"d" that is encountered.
--Christoph
PS: If you expect "ersetzefghi", then of course the replacement string has to be
' ersetz', not ' ersetz ' like it is now. :)
--
"Omniscient? No, not I; but well-informed."
----------------------
XMPP:
ch****************@gmail.com
AOL: 313125838 / cburschka
Key:
http://pgp.mit.edu:11371/pks/lookup?...rch=0x55A52A2A
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla -
http://enigmail.mozdev.org
iD8DBQFI8xqJhbef5VWlKioRAl00AJ9vdgdBW4ktyl4fiSuFyo hqho1q5gCgpKph
exkh8RXm6E4BsQEZ9mD+vXI=
=Hb8E
-----END PGP SIGNATURE-----