php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #7006 preg_replace ( string pattern, array replacement, string subject );
Submitted: 2000-10-04 02:52 UTC Modified: 2013-05-29 09:14 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: joel dot jacobson at mobigym dot se Assigned: maarten (profile)
Status: Closed Package: *General Issues
PHP Version: 4.0.2 OS: Linux 2.4.0-test7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
33 + 42 = ?
Subscribe to this entry?

 
 [2000-10-04 02:52 UTC] joel dot jacobson at mobigym dot se
''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.'' (http://www.php.net/manual/function.preg-replace.php)

I don't agree. The converse is a feature that I would like to be available.

Have a look at the following example:
<?php
$tvPrograms = array ( 'Simpsons', 'Southpark', 'Disney Time' );
$data = '<tr><td>%col%</td><td>%col%</td><td>%col%</td></tr>';
$htmlDoc = preg_replace ( '/%col%/', $tvPrograms, $data );
print $htmlDoc;
?>

This will not work. But wouldn't it be nice if it did?
That is, replace match number n with $tvPrograms[n] in $data.

Thanks in advance for any comments.
Best regards, Joel Jacobson

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-08 10:06 UTC] bigredlinux at yahoo dot com
First of all, using preg_replace here is a sin!  You should be using str_replace, and if you were using str_replace you would not have to worry, since it has this feature.  But, assuming that you did need to match a regular expression, well, then just do array_fill for your search and pass it an array of matches and an array of keys and you will be good to go.  So no, you don't really need this feature.
 [2002-06-08 10:06 UTC] bigredlinux at yahoo dot com
I meant to say "pass it an array of matches and an array of replacements"
 [2010-08-07 01:46 UTC] johannes@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2010-08-07 01:46 UTC] johannes@php.net
.
 [2011-01-02 05:32 UTC] gmblar+php at gmail dot com
<?php

$tvPrograms = array('Simpsons', 'Southpark', 'Disney Time');
$data = '<tr><td>%col%</td><td>%col%</td><td>%col%</td></tr>';
$htmlDoc = preg_replace_callback('/%col%/', function($item) use(&$tvPrograms) {
    return array_shift($tvPrograms);
}, $data);
print $htmlDoc;

?>


# Result:
<tr><td>Simpsons</td><td>Southpark</td><td>Disney Time</td></tr>
 [2013-05-29 09:14 UTC] maarten@php.net
Numerous alternatives available:
- str_replace() if no regex needed
- use array_fill() to pass an array
- use a callback as demonstrated above
etc. etc.
 [2013-05-29 09:14 UTC] maarten@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: maarten
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 04:01:30 2024 UTC