|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 22:00:02 2025 UTC |
<?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>