note 107631 added to ref.mbstring

From: Date: Wed, 22 Feb 2012 19:48:22 +0000
Subject: note 107631 added to ref.mbstring
Groups: php.notes 
Request: Send a blank email to php-notes+get-186283@lists.php.net to get a copy of this message
Looks like mb_str_replace is the most requested missing function from the multibyte string library. I wanted a version of mb_str_replace with as similar a code signature and behavior to str_replace as possible while conforming to the code signature patterns of the mb library and avoiding performance pitfalls like unnecessary concatenations and regular expressions. It's more LOC than others in this documentation thread, but it's also more complete. /** * Multibyte safe version of str_replace. * See http://php.net/manual/en/function.str-replace.php */ function mb_str_replace( $search, $replace, $subject, string $encoding = null, int &$count = null) { if (is_array($subject)) { $result = array(); foreach ($subject as $item) { $result[] = mb_str_replace($search, $replace, $item, $encoding, $count); } return $result; } if (!is_array($search)) { return _mb_str_replace($search, $replace, $subject, $encoding, $count); } $replace_is_array = is_array($replace); foreach ($search as $key => $value) { $subject = _mb_str_replace( $value, $replace_is_array ? $replace[$key] : $replace, $subject, $encoding, $count ); } return $subject; } /** * Implementation of mb_str_replace. Do not call directly. Enforces string parameters. */ function _mb_str_replace( string $search, string $replace, string $subject, string $encoding = null, int &$count = null) { $search_length = mb_strlen($search, $encoding); $subject_length = mb_strlen($subject, $encoding); $offset = 0; $result = ''; while ($offset < $subject_length) { $match = mb_strpos($subject, $search, $offset, $encoding); if ($match === false) { if ($offset === 0) { // No match was ever found, just return the subject. return $subject; } // Append the final portion of the subject to the replaced. $result .= mb_substr($subject, $offset, $subject_length - $offset, $encoding); break; } if ($count !== null) { $count++; } $result .= mb_substr($subject, $offset, $match - $offset, $encoding); $result .= $replace; $offset = $match + $search_length; } return $result; } ---- Server IP: 69.147.83.197 Probable Submitter: 66.220.145.99 ---- Manual Page -- http://www.php.net/manual/en/ref.mbstring.php Edit -- https://master.php.net/note/edit/107631 Del: integrated -- https://master.php.net/note/delete/107631/integrated Del: useless -- https://master.php.net/note/delete/107631/useless Del: bad code -- https://master.php.net/note/delete/107631/bad+code Del: spam -- https://master.php.net/note/delete/107631/spam Del: non-english -- https://master.php.net/note/delete/107631/non-english Del: in docs -- https://master.php.net/note/delete/107631/in+docs Del: other reasons-- https://master.php.net/note/delete/107631 Reject -- https://master.php.net/note/reject/107631 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186283) next »