note 107631 modified in ref.mbstring by danbrown

From: Date: Thu, 10 May 2012 20:27:24 +0000
Subject: note 107631 modified in ref.mbstring by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187808@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.&#13;&#10;&#13;&#10;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.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;/**&#13;&#10; * Multibyte safe version of str_replace.&#13;&#10; * See http://php.net/manual/en/function.str-replace.php&#13;&#10; */&#13;&#10;function mb_str_replace(&#13;&#10; $search,&#13;&#10; $replace,&#13;&#10; $subject,&#13;&#10; string $encoding = null,&#13;&#10; int &#38;$count = null) {&#13;&#10;&#13;&#10; if (is_array($subject)) {&#13;&#10; $result = array();&#13;&#10; foreach ($subject as $item) {&#13;&#10; $result[] = mb_str_replace($search, $replace, $item, $encoding, $count);&#13;&#10; }&#13;&#10; return $result;&#13;&#10; }&#13;&#10;&#13;&#10; if (!is_array($search)) {&#13;&#10; return _mb_str_replace($search, $replace, $subject, $encoding, $count);&#13;&#10; }&#13;&#10;&#13;&#10; $replace_is_array = is_array($replace);&#13;&#10; foreach ($search as $key =&#62; $value) {&#13;&#10; $subject = _mb_str_replace(&#13;&#10; $value,&#13;&#10; $replace_is_array ? $replace[$key] : $replace,&#13;&#10; $subject,&#13;&#10; $encoding,&#13;&#10; $count&#13;&#10; );&#13;&#10; }&#13;&#10; return $subject;&#13;&#10;}&#13;&#10;&#13;&#10;/**&#13;&#10; * Implementation of mb_str_replace. Do not call directly. Enforces string parameters.&#13;&#10; */&#13;&#10;function _mb_str_replace(&#13;&#10; string $search,&#13;&#10; string $replace,&#13;&#10; string $subject,&#13;&#10; string $encoding = null,&#13;&#10; int &#38;$count = null) {&#13;&#10;&#13;&#10; $search_length = mb_strlen($search, $encoding);&#13;&#10; $subject_length = mb_strlen($subject, $encoding);&#13;&#10; $offset = 0;&#13;&#10; $result = &#39;&#39;;&#13;&#10;&#13;&#10; while ($offset &#60; $subject_length) {&#13;&#10; $match = mb_strpos($subject, $search, $offset, $encoding);&#13;&#10; if ($match === false) {&#13;&#10; if ($offset === 0) {&#13;&#10; // No match was ever found, just return the subject.&#13;&#10; return $subject;&#13;&#10; }&#13;&#10; // Append the final portion of the subject to the replaced.&#13;&#10; $result .=&#13;&#10; mb_substr($subject, $offset, $subject_length - $offset, $encoding);&#13;&#10; break;&#13;&#10; }&#13;&#10; if ($count !== null) {&#13;&#10; $count++;&#13;&#10; }&#13;&#10; $result .= mb_substr($subject, $offset, $match - $offset, $encoding);&#13;&#10; $result .= $replace;&#13;&#10; $offset = $match + $search_length;&#13;&#10; }&#13;&#10;&#13;&#10; return $result;&#13;&#10;}&#13;&#10;?&#62; --was-- 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; } http://php.net/manual/en/ref.mbstring.php

« previous php.notes (#187808) next »