#49198 [Bgs]: Incorrect result for .*

From: Date: Mon, 10 Aug 2009 16:04:59 +0000
Subject: #49198 [Bgs]: Incorrect result for .*
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-140871@lists.php.net to get a copy of this message
 ID:               49198
 User updated by:  inf3rno dot hu at gmail dot com
 Reported By:      inf3rno dot hu at gmail dot com
 Status:           Bogus
 Bug Type:         PCRE related
 Operating System: *
 PHP Version:      5.2.10
 New Comment:

Tried out in javascript too:
same result, so I were wrong :-) sorry
<body onload="init();">
php:<br />
<?php
$p1='/.*/';
$test='some text';

function test($m)
{
	echo '"'.$m[0].'"';
	echo '<br />';
	return $m[0];
}

preg_replace_callback($p1,'test',$test);
?><br />
javascript:<br />
<script>
function init()
{
	var p1=<?php echo $p1;?>g;
	var test="<?php echo $test;?>";
	test.replace(p1,function (m)
	{
		document.body.appendChild(document.createTextNode('"'+m+'"'));
		document.body.appendChild(document.createElement('br'));
		return m;
	});
}
</script>
</body>


Previous Comments:
------------------------------------------------------------------------

[2009-08-10 15:42:16] inf3rno dot hu at gmail dot com

I don't agree. How do you explain the same behaviour with the .*$
pattern? 
I think .* have to return a single string not two, it's simple logic.
One match for one string.

------------------------------------------------------------------------

[2009-08-09 11:55:26] rasmus@php.net

I am also pretty sure that this isn't actually a bug.  Doing a
match_all on a non-anchored pattern containing .* is going to match an
empty string.  Remember that * means 0 or more instances of the previous
term.  So, you are doing a match_all for 0 or more characters, and when
you do this non-anchored you are going to get an empty string matching
that.  Change it to .+ (+ means 1 or more) and your patterns start to
make sense and as you will see, the output is what you expect.

------------------------------------------------------------------------

[2009-08-09 11:11:23] rasmus@php.net

If you change:
preg_match_all($p,$t,$m,PREG_SET_ORDER);
to:
preg_match($p,$t,$m);

There is no empty match.  I get this output:

/.*/<br /><div style="color: green;">ok</div><pre>array (
  0 => 'some text',
)</pre><br /><br />/.*$/<br /><div style="color:
green;">ok</div><pre>array (
  0 => 'some text',
)</pre><br /><br />/^.*/<br /><div style="color:
green;">ok</div><pre>array (
  0 => 'some text',
)</pre><br /><br />/^.*$/<br /><div style="color:
green;">ok</div><pre>array (
  0 => 'some text',
)</pre><br /><br />

If you can get this effect with preg_match(), please show how.

------------------------------------------------------------------------

[2009-08-09 10:49:19] inf3rno dot hu at gmail dot com

It's not preg_match_all specific, same bug with every preg function.

------------------------------------------------------------------------

[2009-08-08 21:04:32] pajoye@php.net

Not windows specific

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/49198

-- 
Edit this bug report at http://bugs.php.net/?id=49198&edit=1



Thread (7 messages)

« previous php.bugs (#140871) next »