note 90146 added to function.substr-replace

From: Date: Wed, 08 Apr 2009 02:31:30 +0000
Subject: note 90146 added to function.substr-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-152937@lists.php.net to get a copy of this message
Forget all of the mb_substr_replace() implementations mentioned in this page, they're all buggy. Here is a version that mimics the behavior of substr_replace() exactly: <?php if (function_exists('mb_substr_replace') === false) { function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = null) { if (extension_loaded('mbstring') === true) { $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding); if ($start < 0) { $start = max(0, $string_length + $start); } else if ($start > $string_length) { $start = $string_length; } if ($length < 0) { $length = max(0, $string_length - $start + $length); } else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; } if (($start + $length) > $string_length) { $length = $string_length - $start; } if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); } return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding); } return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length); } } ?> ---- Server IP: 194.8.30.16 Probable Submitter: 87.196.74.255 ---- Manual Page -- http://www.php.net/manual/en/function.substr-replace.php Edit -- https://master.php.net/note/edit/90146 Del: integrated -- https://master.php.net/note/delete/90146/integrated Del: useless -- https://master.php.net/note/delete/90146/useless Del: bad code -- https://master.php.net/note/delete/90146/bad+code Del: spam -- https://master.php.net/note/delete/90146/spam Del: non-english -- https://master.php.net/note/delete/90146/non-english Del: in docs -- https://master.php.net/note/delete/90146/in+docs Del: other reasons-- https://master.php.net/note/delete/90146 Reject -- https://master.php.net/note/reject/90146 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152937) next »