Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51694 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 169 invoked from network); 15 Mar 2011 10:34:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2011 10:34:54 -0000 Authentication-Results: pb1.pair.com header.from=rquadling@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=rquadling@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@gmail.com X-Host-Fingerprint: 209.85.216.42 mail-qw0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:57050] helo=mail-qw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A7/CD-10215-EC04F7D4 for ; Tue, 15 Mar 2011 05:34:54 -0500 Received: by qwi4 with SMTP id 4so293498qwi.29 for ; Tue, 15 Mar 2011 03:34:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=thOQGVJvWaLOopBSyRUTwtlxkgE2xFeguKeZq9eqvGU=; b=m81UoNWwFIRoOWS+hPv5Nfr69XpN1yK7MgeCUOTODaLn7QkDyO/noUItNiJEvPsag2 YYVnGxEANJXydClKT1yN9XUHj4FS+rq2sTBEX9aptMl4OMyuVU+udn9/L2OvRNu4FHze NNk4yt1gUfgbQvggUbKYGfBWN7YPpVD0Z2LAs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; b=e5o8+ACaAtPmMnDsipYv+Khkba0pI47rzZ53958oGWypQ42o+fcCkPZ9jyvVqyspMh 0B8LpU2g+yXanq2jBfu7ARz9smkgBHkYHU1HZ89j1/lXidTc9oZXDjkyKCokbtZr4krC 9Iyl35366MhadkcjqJW2yTcodoK8dx5Q3DheM= Received: by 10.229.78.228 with SMTP id m36mr1367688qck.109.1300185292082; Tue, 15 Mar 2011 03:34:52 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.40.147 with HTTP; Tue, 15 Mar 2011 03:34:32 -0700 (PDT) Reply-To: RQuadling@googlemail.com In-Reply-To: References: Date: Tue, 15 Mar 2011 10:34:32 +0000 Message-ID: To: Hannes Landeholm Cc: Martin Scotta , internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] preg_replace does not replace all occurrences From: rquadling@gmail.com (Richard Quadling) On 15 March 2011 10:32, Richard Quadling wrote: > On 14 March 2011 20:36, Hannes Landeholm wrote: >> What is more likely to be wrong? Your understanding of a specific >> regex pattern (which happens to be full of escapes making it >> incredibly hard to read) or the implementation of preg_replace? >> >> ~Hannes >> >> On 14 March 2011 16:18, Martin Scotta wrote: >>> >>> I chose the simplest example to show the preg_replace behavior, there a= re >>> better (and safer) ways to scape slash characters. >>> Anyways, *is this the expected preg_replace behavior?* >>> >>> =C2=A0Martin >>> >>> >> function test($str) { >>> =C2=A0 =C2=A0static $re =3D '/(^|[^\\\\])\'/'; >>> =C2=A0 =C2=A0static $change =3D '$1\\\''; >>> >>> =C2=A0 =C2=A0echo $str, PHP_EOL, >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0preg_replace($re, $change, $str), PHP_EOL, P= HP_EOL; >>> } >>> >>> test("str '' str"); // bug? >>> test("str \\'\\' str"); // ok >>> test("'str'"); // ok >>> test("\'str\'"); // ok >>> >>> ---- >>> Expected: >>> >>> str '' str >>> str \'\' str >>> >>> str \'\' str >>> str \'\' str >>> >>> 'str' >>> \'str\' >>> >>> \'str\' >>> \'str\' >>> >>> ---- >>> Result: >>> >>> str '' str >>> str \'' str >>> >>> str \'\' str >>> str \'\' str >>> >>> 'str' >>> \'str\' >>> >>> \'str\' >>> \'str\' >>> >>> >>> =C2=A0Martin Scotta >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > Did no one see why the regex was wrong? > > RegexBuddy (a windows app) explains regexes VERY VERY well. The important bit (where the problem lies with regard to the regex) is ... Match a single character NOT present in the list below =C2=AB[^\\\\]=C2=BB A \ character =C2=AB\\=C2=BB A \ character =C2=AB\\=C2=BB The issue is the word _single_. --=20 Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY