note 109580 added to ref.mbstring

From: Date: Mon, 30 Jul 2012 15:56:24 +0000
Subject: note 109580 added to ref.mbstring
Groups: php.notes 
Request: Send a blank email to php-notes+get-189502@lists.php.net to get a copy of this message
There are many mb_str_replace() implementations floating around the net, but I wanted one that is not only efficient, but also works in exactly the same manner as str_replace(). This is what I came up with: <?php /** * Replace all occurrences of the search string with the replacement string. * * @author Sean Murphy <sean@iamseanmurphy.com> * @copyright Copyright 2012 Sean Murphy. All rights reserved. * @license http://creativecommons.org/publicdomain/zero/1.0/ * @link http://php.net/manual/function.str-replace.php * @link http://iamseanmurphy.com/mb-str-replace-the-missing-php-function/ * * @param mixed $search * @param mixed $replace * @param mixed $subject * @param int $count * @return mixed */ if (!function_exists('mb_str_replace')) { function mb_str_replace($search, $replace, $subject, &$count = 0) { if (!is_array($subject)) { // Normalize $search and $replace so they are both arrays of the same length $searches = is_array($search) ? array_values($search) : array($search); $replacements = is_array($replace) ? array_values($replace) : array($replace); $replacements = array_pad($replacements, count($searches), ''); // Make replacements foreach ($searches as $key => $search) { $parts = mb_split(preg_quote($search), $subject); $count += count($parts) - 1; $subject = implode($replacements[$key], $parts); } } else { // Call mb_str_replace for each subject in array, recursively foreach ($subject as $key => $value) { $subject[$key] = mb_str_replace($search, $replace, $value, $count); } } return $subject; } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 97.102.4.134 ---- Manual Page -- http://www.php.net/manual/en/ref.mbstring.php Edit -- https://master.php.net/note/edit/109580 Del: integrated -- https://master.php.net/note/delete/109580/integrated Del: useless -- https://master.php.net/note/delete/109580/useless Del: bad code -- https://master.php.net/note/delete/109580/bad+code Del: spam -- https://master.php.net/note/delete/109580/spam Del: non-english -- https://master.php.net/note/delete/109580/non-english Del: in docs -- https://master.php.net/note/delete/109580/in+docs Del: other reasons-- https://master.php.net/note/delete/109580 Reject -- https://master.php.net/note/reject/109580 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189502) next »