Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

BUG with preg_replace_callback?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

BUG with preg_replace_callback?

Postby bruno31 » Tue Oct 16, 2012 9:45 am

Hello

I'am a visitor of this forum since many years. Today, I registred to post because I found a strange behavior concerning preg_replace_callback:

It is in a piece of code where I reformat HTML code. Functionnaly, it works.

This is the code:
Code: Select all

function remplaceDl($matches) {
   $a =  '<div class=\'spip_documents_'.$matches[1].'  spip_documents\'><img style=\''.$matches[5].'\' width=\''.$matches[3].'\' height=\''.$matches[4].'\' src=\''.$matches[2].'\'><div class=\'spip_doc_titre\'>'.$matches[6].'</div></div>';
      return $a;
}

$patterns_float = "/<dl class='spip_document_\d* spip_documents spip_documents_(left|right|center).*<dt><img src='(.*)'.*width='(.*)'.*height='(.*)'.*style='(.*)'.*<\/dt>\n<dt.*>(.*)<\/dt>\n<\/dl>/iUms";

$html = preg_replace_callback($patterns_float, 'remplaceDl', $html, -1, $count);



Symptom: preg_replace_callback output is empty. the $html variable is reset.
With Eclipse PDT, the variable goes to status "uninitialized".

When I debug step-by-step, the function remplaceDl is well called, with the rigth parameters values, and the function gives the expected result. In my example, it is called 3 times and $count contains 3 also. But $html is reset.

This bug occurs in many configurations:
- even is subject $html does NOT contain text that match with the pattern,
- bug occurs with easyphp, wamp and also with the distant server.

Is it something with $patterns_float?
If I delete <\/dt> at the end, the one in bold below:
$patterns_float = "/<dl class='spip_document_\d* spip_documents spip_documents_(left|right|center).*<dt><img src='(.*)'.*width='(.*)'.*height='(.*)'.*style='(.*)'.*<\/dt>\n<dt.*>(.*)<\/dt>\n<\/dl>/iUms";
, it does not bug but the returning result is not the one I expect.

I tried to decrease the content of subject $html. And if there are only few tens of bytes, it does not bug. I wonder if there is something with the stack?

Note that I have several others preg_replace_callback after this piece of code, that handle the same $html, and it does not bug.

What could it be?

THANKS for your :help:
bruno31
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Oct 16, 2012 7:29 am

Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.