プロが教える店舗&オフィスのセキュリティ対策術

PHPの正規表現で、使える文字が数字またはコンマの場合の書き方
'/^[0-9,]+$/'
に使える文字として、NULL文字も追加したいのですが、
その書き方がよく分かりません。
正規表現としては、どのように書けばいいのでしょうか?

A 回答 (3件)

>5.The following Perl escape sequences are not supported: \l, \u, \L, \U. In fact these are implemented by Perl's general string-handling and are not part of its pattern matching engine.



すまん、やっぱりconfigureでは無理かも。
    • good
    • 0

以下回答ではなく殴り書きのメモ。



未検証:

http://www.php.net/manual/en/reference.pcre.patt …

>Though binary zero characters are supported in the subject string, they are not allowed in a pattern string because it is passed as a normal C string, terminated by zero. The escape sequence "\x00" can be used in the pattern to represent a binary zero.

#俺自身は「文字」単位でなく「バイト」単位でやることに大反対。一応メモだけ。

#strrpos関数とか使って無理やり実装するのも好きじゃないしなあ

http://unicode.org/reports/tr18/#Hex_notation

とかで出来そうな気がするんだけどなあ。

from 【U+0000】 to U+10FFFF)

#独自ビルドのPHP6で失敗。どうやらPCREはデフォルトではUnicodeをサポートせず、configureで設定しなければならないようだが…そういう設定のPHPを載っけてくれているサーバってどれくらいあるんだろう。

あまりコンパイルした事ないからどこで設定するのかわからん > PCREのconfigure
    • good
    • 0

>NULL文字も追加したい



ヌルは文字列ではないので正規表現では評価できません。
PHPはあいまいな評価がされるため""と0と"0"とNULLは
似ているようで違いますのでご注意ください。

<?

$str=NULL;check($str);
$str="";check($str);
$str=0;check($str);
$str="0";check($str);

function check($str=null){
if(is_null($str)) print 0;
if(isset($str)) print 1;
if(empty($str)) print 2;
if($str=="") print 3;
if($str==="") print 4;
if($str==0) print 5;
if($str===0) print 6;
if($str=="0") print 7;
if($str==="0") print 8;
if(preg_match("/^$/",$str)) print 9;
print "<br>\n";
}

?>
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!