note 126447 added to function.str-replace

From: Date: Tue, 21 Sep 2021 21:35:26 +0000
Subject: note 126447 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-216989@lists.php.net to get a copy of this message
Addtition to nikolaz dot tang answer: If your input array may ever contain Unicode symbols, do not forget to add JSON_UNESCAPED_UNICODE flag to json_encode(), or it won't work. Also you can add JSON_UNESCAPED_SLASHES if you want to str_replace some slashes. And as mrrehbein at gmail dot com noticed, there were second argument "true" in json_decode missed (without it json_decode returns objects, not arrays) Recommended way for multidimensional arrays: <?php function str_replace_json($search, $replace, $subject){ return json_decode(str_replace($search, $replace, json_encode($subject, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)), true); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 128.70.4.219) ---- Manual Page -- https://php.net/manual/en/function.str-replace.php Edit -- https://main.php.net/note/edit/126447 Del: integrated -- https://main.php.net/note/delete/126447/integrated Del: useless -- https://main.php.net/note/delete/126447/useless Del: bad code -- https://main.php.net/note/delete/126447/bad+code Del: spam -- https://main.php.net/note/delete/126447/spam Del: non-english -- https://main.php.net/note/delete/126447/non-english Del: in docs -- https://main.php.net/note/delete/126447/in+docs Del: other reasons-- https://main.php.net/note/delete/126447 Reject -- https://main.php.net/note/reject/126447 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216989) next »