Re: [PHP4BETA] perl compatible regex search&replace behaving oddly... bug?

From: Date: Tue, 18 Jan 2000 18:19:50 +0000
Subject: Re: [PHP4BETA] perl compatible regex search&replace behaving oddly... bug?
References: 1 2 3 4 5 6  Groups: php.version4 
Request: Send a blank email to php-version4+get-9253@lists.php.net to get a copy of this message
On Tue, 18 Jan 2000, Alexander Fordyce wrote: I realize that the parse error occurs because it's not finding anything nice and executable the second time around. Sometimes it produces an ugly binary-looking garbage value for the second variable -- it's a bit unpredictable. No, the parse error occurs because you used $ in a string without escaping it with \.
Thanks for the suggestion, but that's not it. The error occurs whether or not the $ is backslashed. Incidentally, if I remove the /e flag (and backslash the $), I get: $var1 = "contents1"; $var2 = "contents2"; $str = "<HTML><BODY> this is one variable: <B>#var1#</B>,<BR> and this is another: <B>#var2#</B>. </BODY></HTML>"; $str = preg_replace("/#([^#]+)#/", "\$\\1", $str); echo $str; - - output: - - this is one variable: $var1, and this is another: $sÀG. ..well, it's clear why that isn't evaluating. The garbage is unpredictable. This occurs with any \\n reference in the implicit loop that preg_replace executes -- whether or not there's a $ in the replacement string or not (see example below).
It seems like it should work, as that's the way it's presented in the documentation. However, the example from the manual which is supposed to upper-case html tags produces similar garbage when I copy and paste the code: - - - - - - - - - - - - - - - $html_body = "<P>just some <strong>nice</strong> <b>simple</b> text</P>"; print preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body); - - output: - - <P>just some rONG>NIcniceb> TEXT</ simple textó(; - - - - - - - - - - - - - - - Hmm, I just ran the exact same code here and it works fine. Perhaps you could try again?
This I can reproduce. This time: Parse error: parse error in /tests/preg_test.html on line 0 <P>just some rONG>NIcniceb> TEXT</ simple text¥$/ Perhaps there's something up with my installation? That would be annoying, as it seemed as if everything had gone as it should -- it compiled cleanly and without incident, and it's running fine. I have all kinds of things compiled in, btw... mhash, mcrypt, xml, unixodbc... any conflicts possible? I'm on Redhat 6.1 with all updates applied before installing php. Thanks, Alex

« previous php.version4 (#9253) next »