[PHP-users 12730] Re: mb_regex_set_options()の引数って?
Yasuo Ohgaki
php-users@php.gr.jp
2003年1月22日 12:39:42 +0900
大垣です。
Moriyoshi Koizumi wrote:
> 知られているバグ:
>> $ echo '<?php
> print mb_ereg_replace("\$", "--", "abc\ndef\n", "");
> ?>' | php
>> 結果:
> abc----
> def----
>
# CGIには-rオプションがないので小泉さんのことですから
# 敢えて-rオプションを使わなかったのだと思いますが
ruby, perlと同じように直接コードを渡すには
$ php -r 'print mb_ereg_replace("\$", "--", "abc\ndef\n", "");'
でできます。結果は同じです。
ereg_replaceを使うと最後の\nしか置換しません。
(ビルトインregexライブラリ使用)
$ php -r 'print ereg_replace("\$", "--", "abc\ndef\n");'
abc
def
--
preg_replaceだと
$ php -r 'print preg_replace("/\$/m", "--", "abc\ndef\n");'
abc--
def--
--
$ php -r 'print preg_replace("/\$/", "--", "abc\ndef\n"););'
abc
def--
--
> $ ruby -e 'print "abc\ndef\n".gsub(/$/, "--");'
>> 結果:
> abc--
> def--
>> $ perl -e '$_ = "abc\ndef\n"; $_ =~ s/$/--/mg; print $_;'
>> 結果:
> abc--
> def--
> --
>> この3つの挙動は異りますので注意してください。
> これはバグでしょうか?
興味深い結果ですね...
個人的にはrubyの結果が正しいように思えますが、正規表現
のスペシャリストでは無いので、あくまで個人的な感想です。
少なくともPCRE(Perl Compatible Regex)はPerl互換
なようです。
--
Yasuo Ohgaki