note 58935 added to function.array-walk

From: Date: Mon, 21 Nov 2005 14:09:10 +0000
Subject: note 58935 added to function.array-walk
Groups: php.notes 
Request: Send a blank email to php-notes+get-98885@lists.php.net to get a copy of this message
In response to 'ibolmo', this is an extended version of string_walk, allowing to pass userdata (like array_walk) and to have the function edit the string in the same manner as array_walk allows, note now though that you have to pass a variable, since PHP cannot pass string literals by reference (logically). <?php function string_walk(&$string, $funcname, $userdata = null) { for($i = 0; $i < strlen($string); $i++) { # NOTE: PHP's dereference sucks, we have to do this. $hack = $string{$i}; call_user_func($funcname, &$hack, $i, $userdata); $string{$i} = $hack; } } function yourFunc($value, $position) { echo $value . ' '; } function yourOtherFunc(&$value, $position) { $value = str_rot13($value); } # NOTE: We now need this ugly $x = hack. string_walk($x = 'interesting', 'yourFunc'); // Ouput: i n t e r e s t i n g string_walk($x = 'interesting', 'yourOtherFunc'); echo $x; // Output: vagrerfgvat ?> Also note that calling str_rot13() directly on $x would be much faster ;-) just a sample. ---- Manual Page -- http://www.php.net/manual/en/function.array-walk.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58935 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58935&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58935&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98885) next »