note 60628 added to function.str-replace

From: Date: Thu, 12 Jan 2006 17:49:57 +0000
Subject: note 60628 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-101643@lists.php.net to get a copy of this message
Note the unfortunate consequence of beeing unable to format str_replace like this: <?php pairs = array( 'this' => 'to this', 'this one' => 'to this one', /* many lines here */ ); str_replace(pairs, context); ?> The correct form requires two arrays with no visual support for the code reader that would help him to match the actual replacement pairs in a natural and visually seamless manner. In this aspect the code above is by far more maintainable and rugged. The simple solution is as follows: <?php pairs = array( 'this' => 'to this', 'this one' => 'to this one', /* many lines here */ ); str_replace(array_keys(pairs), array_values(pairs), context); ?> Note that the overhead brought by creating two new arrays for purely aesthetic reasons might be a waste of perfectly good CPU time. ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60628 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60628&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60628&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101643) next »