note 117026 added to function.str-replace

From: Date: Fri, 03 Apr 2015 13:25:28 +0000
Subject: note 117026 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-202158@lists.php.net to get a copy of this message
nikolaz dot tang at hotmail dot com's solution of using json_encode/decode is interesting, but a couple of issues to be aware of with it. <?php // From: nikolaz dot tang at hotmail dot com's post function str_replace_json($search, $replace, $subject){ return json_decode(str_replace($search, $replace, json_encode($subject))); } ?> json_decode will return objects, where arrays are probably expected. This is easily remedied by adding 2nd parameter 'true' to json_decode. $search and $replace could contain strings that match json encoding, which will either change the structure returned by this method, or break the json. ie: <?php var_dump(str_replace_json('":"', '","', ['this' => 'stuff'])); var_dump(str_replace_json('this":"', 'this" : "thing", "with":"', ['this' => 'stuff'])); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 71.13.171.2 ---- Manual Page -- http://php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/117026 Del: integrated -- https://master.php.net/note/delete/117026/integrated Del: useless -- https://master.php.net/note/delete/117026/useless Del: bad code -- https://master.php.net/note/delete/117026/bad+code Del: spam -- https://master.php.net/note/delete/117026/spam Del: non-english -- https://master.php.net/note/delete/117026/non-english Del: in docs -- https://master.php.net/note/delete/117026/in+docs Del: other reasons-- https://master.php.net/note/delete/117026 Reject -- https://master.php.net/note/reject/117026 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202158) next »