Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51683 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31254 invoked from network); 14 Mar 2011 15:34:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Mar 2011 15:34:05 -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.170 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.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:36186] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1C/42-16045-C653E7D4 for ; Mon, 14 Mar 2011 10:34:05 -0500 Received: by qyk32 with SMTP id 32so1345104qyk.8 for ; Mon, 14 Mar 2011 08:34:02 -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=fSdGIEORG7mnBXjFC5ZBR5o0uzgFBAPLH44CJTuF44g=; b=gLQQDeb2o25Up5PU+9nEesueacURSDTAmv5gwQJVXRAZ5oXEK49UafbTG/Gn7+M+ih XBBW/mVxUJn9sK9wOYy7DPfIT3i9q8MxRFvS8wSDoJDY4R6oo1v0kkD5dufmU3UYO6AD JCkZ9otRPuWdDl82PM1KupfzckAIJiM6RGJgU= 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=KtLDMk/OqBWHxqcGbZUB8RPBhSiB7su1OVYOaMIoqjfbVJ91MI34Y5tItV/MLKbnUH xcDz73Q8YPH+2zsbKRSGXFxNhSrsdyADxffsL3I09Gyz1QjXcJeD2yltrsqn6cZj1gWL Y3OfI1es5o67HwcepVu3kK/S5tPbheFULUdCc= Received: by 10.229.62.8 with SMTP id v8mr10205811qch.33.1300116842253; Mon, 14 Mar 2011 08:34:02 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.40.147 with HTTP; Mon, 14 Mar 2011 08:33:42 -0700 (PDT) Reply-To: RQuadling@googlemail.com In-Reply-To: References: Date: Mon, 14 Mar 2011 15:33:42 +0000 Message-ID: To: Martin Scotta Cc: PHP internals 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 14 March 2011 15:18, Martin Scotta wrote: > 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, PHP= _EOL; > } > > test("str '' str"); // bug? > test("str \\'\\' str"); // ok > test("'str'"); // ok > test("\'str\'"); // ok > Your regex is ... (^|[^\\\\])' Options: case insensitive; ^ and $ match at line breaks Match the regular expression below and capture its match into backreference number 1 =C2=AB(^|[^\\\\])=C2=BB Match either the regular expression below (attempting the next alternative only if this one fails) =C2=AB^=C2=BB Assert position at the beginning of a line (at beginning of the string or after a line break character) =C2=AB^=C2=BB Or match regular expression number 2 below (the entire group fails if this one fails to match) =C2=AB[^\\\\]=C2=BB 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 Match the character =E2=80=9C'=E2=80=9D literally =C2=AB'=C2=BB I think [^\\\\] is wrong and you want it to be ... (?!\\\\) or (?!\\{2}) With that, the output is ... str '' str str \'\' str str \'\' str str \\'\\' str 'str' \'str\' \'str\' \\'str\\' --=20 Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY