php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dodothedreamer at gmail dot com
New email:
PHP Version: OS:

 

 [2015-03-26 14:21 UTC] dodothedreamer at gmail dot com
Description:
------------
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="http://tivonim-blog.co.il/%D7%A7%D7%A0%D7%99%D7%99%D7%93%D7%9C%D7%9A-%D7%98%D7%91%D7%A2%D7%95%D7%A0%D7%99-%D7%9E%D7%95%D7%A9%D7%9C%D7%9D-%D7%9E%D7%A8%D7%A7/">מתכון לקניידלך טבעוני </a>מהבלוג "לא על החסה לבדה" </figcaption>
</figure>

<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="http://veg.anonymous.org.il/art283.html">מרק קיינדלך</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="http://veg.anonymous.org.il/art552.html">תחתיות ארטישוק ממולאות קינואה</a></strong></p>

<p><strong style="font-size: 10pt;"><a href="http://veg.anonymous.org.il/art627.html">פטריות ממולאות בקרם אגוזים</a></strong></p>

<p><a href="http://veg.anonymous.org.il/art347.html" 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>

aaaaaaaaaa

');


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

> 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
preg_replace_callback().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC