mb_ereg_replace

(PHP 4 >= 4.2.0)

mb_ereg_replace -- Replace regular expression with multibyte support

Description

string mb_ereg_replace ( string pattern, string replacement, string string [, string option] )

mb_ereg_replace() scans string for matches to pattern, then replaces the matched text with replacement and returns the result string or FALSE on error. Multibyte character can be used in pattern.

Matching condition can be set by option parameter. If i is specified for this parameter, the case will be ignored. If x is specified, white space will be ignored. If m is specified, match will be executed in multiline mode and line break will be included in '.'. If p is specified, match will be executed in POSIX mode, line break will be considered as normal character. If e is specified, replacement string will be evaluated as PHP expression.

The internal encoding or the character encoding specified in mb_regex_encoding() will be used as character encoding.

See also: mb_regex_encoding(), mb_eregi_replace().


add a note add a note User Contributed Notes
squeegee
01-Nov-2006 11:41
well, if you just calculated the length of the find and replace strings once instead of on every loop, it would likely speed it up a lot.
mpnicholas [@t] gmail (dot) com
10-Jul-2006 06:09
Regarding the mb_str_ireplace() function: I benchmarked it against mb_eregi_replace() for single-character substitution, and it was significantly slower. Despite avoiding the ereg call, I think the while loop ends slowing you down too much for this to be practical.
vondrej(at)gmail(dot)com
27-Feb-2006 07:47
Are you looking for htmlentities() for multibyte strings? This might help you - it just replace <, >, ", '

/**
 *  Multibyte equivalent for htmlentities() [lite version :)]
 *
 * @param string $str
 * @param string $encoding
 * @return string
 **/
function mb_htmlentities($str, $encoding = 'utf-8') {
   mb_regex_encoding($encoding);
   $pattern = array('<', '>', '"', '\'');
   $replacement = array('&lt;', '&gt;', '&quot;', '&#39;');
   for ($i=0; $i<sizeof($pattern); $i++) {
       $str = mb_ereg_replace($pattern[$i], $replacement[$i], $str);
   }
   return $str;
}
faxe at neostrada dot pl
10-Aug-2005 06:52
A simple mb_str_ireplace() implementation - a faster (?) replacement for non-regexp multi-byte string replacement:

[code]
function mb_str_ireplace($co, $naCo, $wCzym)
{
   $wCzymM = mb_strtolower($wCzym);
   $coM    = mb_strtolower($co);
   $offset = 0;
  
   while(($poz = mb_strpos($wCzymM, $coM, $offset)) !== false)
   {
       $offset = $poz + mb_strlen($naCo);
       $wCzym = mb_substr($wCzym, 0, $poz). $naCo .mb_substr($wCzym, $poz+mb_strlen($co));
       $wCzymM = mb_strtolower($wCzym);
   }
  
   return $wCzym;
}[/code]