Edit report at http://bugs.php.net/bug.php?id=7006&edit=1
ID: 7006
Comment by: gmblar+php at gmail dot com
Reported by: joel dot jacobson at mobigym dot se
Summary: preg_replace ( string pattern, array replacement,
string subject );
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: Linux 2.4.0-test7
PHP Version: 4.0.2
Block user comment: N
Private report: N
New Comment:
<?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>
Previous Comments:
------------------------------------------------------------------------
[2010-08-07 01:46:47] [email protected]
.
------------------------------------------------------------------------
[2002-06-08 10:06:49] bigredlinux at yahoo dot com
I meant to say "pass it an array of matches and an array of
replacements"
------------------------------------------------------------------------
[2002-06-08 10:06:14] 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.
------------------------------------------------------------------------
[2000-10-04 02:52:34] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=7006&edit=1