Req #7006 [Opn->Csd]: preg_replace ( string pattern, array replacement, string subject );
| From: | maarten@php.net | Date: | Wed, 29 May 2013 09:14:23 +0000 |
| Subject: | Req #7006 [Opn->Csd]: preg_replace ( string pattern, array replacement, string subject ); | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-179203@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=7006&edit=1
ID: 7006
Updated by: maarten@php.net
Reported by: joel dot jacobson at mobigym dot se
Summary: preg_replace ( string pattern, array replacement,
string subject );
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: *General Issues
Operating System: Linux 2.4.0-test7
PHP Version: 4.0.2
-Assigned To:
+Assigned To: maarten
Block user comment: N
Private report: N
New Comment:
Numerous alternatives available:
- str_replace() if no regex needed
- use array_fill() to pass an array
- use a callback as demonstrated above
etc. etc.
Previous Comments:
------------------------------------------------------------------------
[2011-01-02 05:32:58] 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>
------------------------------------------------------------------------
[2010-08-07 01:46:47] johannes@php.net
.
------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=7006&edit=1