|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69309 preg_replace return NULL on certain string
Submitted: 2015-03-26 14:21 UTC Modified: 2015-03-26 14:47 UTC
From: dodothedreamer at gmail dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 5.4.39 OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: dodothedreamer at gmail dot com
New email:
PHP Version: OS:


 [2015-03-26 14:21 UTC] dodothedreamer at gmail dot com
I saw this bug on PHP version 5.4.32 and also on 5.5.9-1ubuntu4.7
When you enter a certain string ( see the test script) to preg_replace, it returns NULL. Some small changes in the string don't cause this error, for example if you delete one of the "a" characters from the end, then the bug doesn't happen.

preg_last_error() return 2.

Increasing pcre.backtrack_limit and pcre.recursion_limit does not help ( the regex engine seems to get stuck in a loop for a long time )

Test script:
$returnValue = preg_replace('#\\<figure +.*?\\>\\<img +(.+?)/\\>.*?\\<figcaption\\>\\<a href="(.+?)".*?\\>(.*?)\\</a\\>\\</figcaption\\>.*?\\</figure\\>#s', '<figure class="caption"><a href="$2"><img $1/></a> <figcaption><a href="$2">$3</a></figcaption> </figure>', '<figure class="caption" style="float:left"><img alt="מתכון לקניידלך טבעוני מהבלוג לא על החסה לבדה " onmouseup="this.src="graphic/img1530.jpg"" src="img1530.jpg" />
<figcaption><a href="">מתכון לקניידלך טבעוני </a>מהבלוג "לא על החסה לבדה" </figcaption>

<p>מנות ראשונות, סלטים ותוספות:</p>

<p><strong style="font-size: 10pt;"><a href="art160.html">סלט פלפלים קלויים</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="art161.html">סלט חסה, סלרי ואגוזי מלך</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="art162.html">סלט סלק</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="art198.html">אספרגוס בשזיפים ושקדים</a></strong></p>

<p><span style="font-size: 10pt;">מרקים:</span></p>

<p><strong style="font-size: 10pt;"><a href="">מרק קיינדלך</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="art178.html">מרק בצל</a></strong></p>

<p><span style="font-size: 10pt;">מנות עיקריות:</span></p>

<p><strong style="font-size: 10pt;"><a href="">תחתיות ארטישוק ממולאות קינואה</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="">פטריות ממולאות בקרם אגוזים</a></strong></p>

<p><a href="" style="font-size: 10pt;"><b>קינואה בעשבי תיבול</b></a></p>

<p><strong style="font-size: 10pt;"><a href="art229.html">תפוח-אדמה מוקרם בחלב קוקוס ותרד</a></strong></p>

<p><span style="font-size: 10pt;">קינוחים:</span></p>




Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-26 14:47 UTC]
-Status: Open +Status: Not a bug
 [2015-03-26 14:47 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

> preg_last_error() return 2.

That is PREG_BACKTRACK_LIMIT_ERROR. Increasing pcre.backtrack_limit
to 10,000,000 is sufficient to process the provided test script.

However, I suggest you simplify the regex in combination with
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 19:01:28 2024 UTC