Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Created November 29, 2011 11:34
Show Gist options
  • Select an option

  • Save fetus-hina/1404506 to your computer and use it in GitHub Desktop.

Select an option

Save fetus-hina/1404506 to your computer and use it in GitHub Desktop.
mb_str_replace() 説明ページ用 str_replace エミュレート例
<?php
$subject = 'hogefuga';
$table = array('hoge' => 'foo',
'fuga' => 'bar',
'piyo' => 'baz',
'foo' => '***',
'bar' => '+++',
'baz' => '---');
// mb_str_replace の置き換え動作
var_dump(mb_str_replace(array_keys($table), array_values($table), $subject));
// str_replace の置き換え動作
var_dump(str_replace(array_keys($table), array_values($table), $subject));
// mb_str_replace を使って str_replace と同じ動作を行う
// 関数呼び出しを配列で指定せず、順番に適用します
$tmp = $subject;
foreach(array_keys($table) as $key) {
$tmp = mb_str_replace($key, $table[$key], $tmp);
}
var_dump($tmp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment