php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26508 preg_replace() misbehavior with multidimensional arrays as "subject" paramenter
Submitted: 2003-12-03 09:04 UTC Modified: 2003-12-03 09:38 UTC
From: putaso at nospam dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.3 OS: Windows 2000
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: putaso at nospam dot com
New email:
PHP Version: OS:

 

 [2003-12-03 09:04 UTC] putaso at nospam dot com
Description:
------------
<?php
/*
	preg_replace() misbehavior.
	Treats Multidimensinal arrays in "subject" in a really weird way.
*/

class object {};
$alpha = new object;
$alpha->beta = "alpha-beta";
$alpha->gama->x = "hola";

$array = array("a" => "abc", "azw", array("a", "b", array("af")), $alpha->beta, $alpha->gama );
print_r (preg_replace("/[a|e|i|o|u]/", "#", $array));


/*
and this is what i get:
Array
(
    [a] => #bc
    [0] => #zw
    [1] => Arr#y
    [2] => #lph#-b#t#
    [3] => Obj#ct
)


Is is correct that i get an array element called "Arr#y" and Obj#ct??
shouldn't preg_replace go through the arrays recursively?
The manual is not very clear about that:

"If subject is an array, then the search and replace is performed on every entry of subject , and the return value is an array as well."

anyways...
I don't think that if the function finds an array or an object, treats it as string "Array" and string "Object",
*/

?>



Reproduce code:
---------------
<?php
class object {};
$alpha = new object;
$alpha->beta = "alpha-beta";
$alpha->gama->x = "hola";

$array = array("a" => "abc", "azw", array("a", "b", array("af")), $alpha->beta, $alpha->gama );
print_r (preg_replace("/[a|e|i|o|u]/", "#", $array));
?>



Expected result:
----------------
Array
(
    [a] => #bc
    [0] => #zw
    [1] => Array
        (
            [0] => #
            [1] => b
            [2] => Array
                (
                    [0] => #f
                )

        )

    [2] => #lph#-b#t#
    [3] => stdClass Object
        (
            [x] => hol#
        )

)



Actual result:
--------------
Array
(
    [a] => #bc
    [0] => #zw
    [1] => Arr#y
    [2] => #lph#-b#t#
    [3] => Obj#ct
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-03 09:11 UTC] derick@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_replace doesn\'t do multidimensional arrays so arays get converted to \"Array\" and objects to \"Object\".
 [2003-12-03 09:32 UTC] putaso at nospam dot com
Shouldn't it be more clear in the manual?

anyways.. i don't think you should get "Array" and "object" strings then if now multidimensional arrays are supported.

Should I create a new bug as a Documentation Bug, or something like that?

thanks to you for taking the time for replying.
 [2003-12-03 09:38 UTC] derick@php.net
Nope, I just added some clarification to the documentation for this.
 [2003-12-03 13:48 UTC] putaso at ospam dot com
ok. thanks then.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Sep 19 21:00:02 2025 UTC