php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25042 pattern emtpy and replacement array
Submitted: 2003-08-11 09:44 UTC Modified: 2003-08-13 03:57 UTC
From: michiel at tincan dot co dot uk Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 4.3.2 OS: Linux
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: michiel at tincan dot co dot uk
New email:
PHP Version: OS:

 

 [2003-08-11 09:44 UTC] michiel at tincan dot co dot uk
Description:
------------
When upgrading to 4.3.2 this started to happen:

whenever I use

$string = preg_replace("/\[$pattern\]/",$replacement,$string)

and the $pattern is empty or does not exist, but the replacement is an array, my string is made empty.


'. /configure' '--prefix= /usr /local' '--with-apxs= /usr /local /apache-1.3.27 /bin /apxs' '--with-config-file-path= /etc' '--with-mysql= /usr' '--with-ldap' '--enable-sysvshm=yes' '--enable-sysvsem=yes' '--with-gd= /usr' '--with-gettext' '--with-xml' '--with-imap' '--enable-sockets' '--enable-ftp' '--with-regex=system' '--enable-track-vars=yes' '--enable-force-cgi-redirect=yes' '--enable-memory-limit=yes' '--enable-debug=no'

Register Globals is "on"

Reproduce code:
---------------
  $data = array(
    "field1" => "somevalue",
    "field2" => array("hmm","whassup"),
  );

  $html = '<b>[field1]</b>
  ,<h1>[field3]</h1>';

  while (list($field,$fielddata) = each ($data)) {
    $html = preg_replace("/\[$field\]/",$fielddata,$html);
  }
  print $html;
# will print nothing. however if you comment out the line "field2" etc above, it will print something


Expected result:
----------------
output should be :
<b>somevalue</b>
, <h1>[field3]</h1>

Actual result:
--------------
no output, the string $html is empty

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-11 11:45 UTC] sniper@php.net
Try increasing your error_reporting level:

Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement in an array. 
 [2003-08-12 04:12 UTC] michiel at tincan dot co dot uk
Well, you may have tagged it bogus, but it's no use to have a "Warning", when the behaviour should be different. The pattern is not matched so regardless of the (type and value of the) replacement, the replacement should not take place. So I still think this is a bug.
 [2003-08-12 09:06 UTC] sniper@php.net
From the friendly manual page for preg_replace():

"If pattern  is an array and replacement is a string, then this replacement string is used for every value of pattern. The converse would not make sense, though."

It errors out and does nothing in the latter case.

 [2003-08-12 09:12 UTC] michiel at tincan dot co dot uk
Yes, exactly. 

It errors out and does nothing in the latter case.

My bug is that it doesn't error out, but does something, ie it wipes the string. Sorry, maybe I don't get it, but I haven't been convinced about the bogus-ness of this bug yet.
 [2003-08-12 09:18 UTC] sniper@php.net
With latest CVS snapshot, it errors out and doesn't do anything.

 [2003-08-13 03:57 UTC] michiel at tincan dot co dot uk
thanks. that's great. Just curious, was that already fixed, or did you fix it just now?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 23:01:31 2024 UTC