php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #45440 preg_replace does not do replacements inside $subject
Submitted: 2008-07-06 15:22 UTC Modified: 2008-11-05 12:16 UTC
From: aremo at ngi dot it Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2008-07-06 15:22 UTC] aremo at ngi dot it
Description:
------------
Probably a documentation bug. The manual page for preg_replace (especially in the description of $subject) makes it sound as though the replacements were made directly on $subject, while this is not the case. The replacements are only in the returned string.


Reproduce code:
---------------
#!/usr/bin/php
<?php   
        $subject = "big";
        $result  = preg_replace('#i#','u',$subject);
        echo $subject.' '.$result;
?>


Expected result:
----------------
If the replacement took place in $subject, the program should output "bug bug".


Actual result:
--------------
The program outputs "big bug".

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-05 10:46 UTC] vrana@php.net
preg_replace() clearly states that the replaced string is returned. Moreover, the $subject is not passed by reference so it can not be modified from inside the function.
 [2008-11-05 12:16 UTC] aremo at ngi dot it
I guess "clearly" is in the eye of the beholder. I, for one, was certainly misled by the chosen phraseology.

The manual does say that the new subject is returned, but still makes it sound as the replacements are made directly on $subject (e.g. "If subject is an array, then the search and replace is performed on every entry of subject").

Of course $subject is passed by value, so that can't be; but a well written *reference* manual does not leave stuff as an exercise to the reader.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 23:01:31 2024 UTC