Created
November 29, 2011 11:34
-
-
Save fetus-hina/1404506 to your computer and use it in GitHub Desktop.
mb_str_replace() 説明ページ用 str_replace エミュレート例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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