php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53134 preg_replace_callback doesn't work with the "*?" (lazy quantification)
Submitted: 2010-10-22 07:10 UTC Modified: 2010-10-25 03:43 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: xianhua dot zhou at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.3 OS: Ubuntu 10.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xianhua dot zhou at gmail dot com
New email:
PHP Version: OS:

 

 [2010-10-22 07:10 UTC] xianhua dot zhou at gmail dot com
Description:
------------
See the script below, if the $data is too big, then the script will be failed, but if the regular expression from:
<pre>
!<script>([\s\S]*?)</script>!i
</pre>

changed to 

</pre>
!<script>([\s\S]*)</script>!i
<pre>

Then, it works.


Test script:
---------------
<?php
$data = 'blabla <script>' . str_repeat("a", 1024 * 100) . '</script>';
$data = preg_replace_callback('!<script>([\s\S]*?)</script>!i', function($matches) {
    return $matches[1];
}, $data);
echo strlen($data);
?>

Expected result:
----------------
102407

Actual result:
--------------
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-22 07:17 UTC] xianhua dot zhou at gmail dot com
With the "*?" pattern, if the line

"str_repeat("a", 1024 * 100)"

changed to

"str_repeat("a", 1024 * 97)"

Then, it also works.

But if it's changed to "str_repeat("a", 1024 * 98)", then it doesn't work.
 [2010-10-23 22:24 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2010-10-23 22:24 UTC] felipe@php.net
This is because the backtrack limit. Try setting a major backtrack_limt, example: ini_set('pcre.backtrack_limit', PHP_INT_MAX);


Not a bug, it's an expected behavior from PCRE. :)
 [2010-10-25 03:43 UTC] xianhua dot zhou at gmail dot com
Thanks for the reply. In this case, it would be better if php can show an 
error/warning message instead 
of return NULL. e.g. like the "memory_limit" behavior.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 19 19:00:01 2025 UTC