忍者ブログ

カウンター

プロモーション

カレンダー

04 2024/05 06
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

AntinomyMy の実験室

   私のWEBアプリ実験室です!

ブログ内検索

楽天でお買い物

twitter

最新トラックバック

最新コメント

忍者アナライズ

ウェザーニュース

バーコード

本を買う

アクセス解析

Google+

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。


  • 2024/05/06 07:29

PHPでアドレスチェックは正規表現使わないでもフィルター関数(filter_var)で出来る部分があるのを知った

PHP の調べ物

メモです@x@

filter_var() という関数を使うと
正規表現を使わないでも
http://www.faqs.org/rfcs/rfc2396.html
で規格化されているスキーマ(scheme) に関しては
チェックできるのが分かった!
http://php.net/manual/ja/function.filter-var.php

このfilter_var()という関数は
メールアドレスや、URI 、IPアドレスにも対応していて
細かく指定をするのならば
http://php.net/manual/ja/filter.filters.validate.php

にあるID だけでなく、フラグの部分も第三引数として追加すれば
もっと細かく指定できるみたいです@@

それで一応ちょっとPHP 公式のメモにあったのに少し付け足してテストしてみた。

$filterTestArray=array(
        
'ftp://ftp.is.co.za.example.org/rfc/rfc1808.txt',
        
'gopher://spinaltap.micro.umn.example.edu/00/Weather/California/Los%20Angeles',
        
'http://www.math.uio.no.example.net/faq/compression-faq/part1.html',
        
'mailto:mduerst@ifi.unizh.example.gov',
        
'news:comp.infosystems.www.servers.unix',
        
'telnet://melvyl.ucop.example.edu/',
        
'http://www.ietf.org/rfc/rfc2396.txt',
        
'ldap://[2001:db8::7]/c=GB?objectClass?one',
        
'mailto:John.Doe@example.com',
        
'news:comp.infosystems.www.servers.unix',
        
'tel:+1-816-555-1212',
        
'telnet://192.0.2.16:80/',
        
'urn:oasis:names:specification:docbook:dtd:xml:4.1.2',
        
'http://aaaaaaaa.aa.aa',
        
'http://aaaaaaaa.aa.aa/',
        
'callto:123-456-789',
        
'callto:123-456-789/',
        
'skype:111-222-444',
        
'skype:123456789'
);
foreach(
$filterTestArray as $oneFilterTestString){
    echo 
'['.$oneFilterTestString.']=><br>';
    if(
filter_var($oneFilterTestString,FILTER_VALIDATE_URL)){
        echo 
'['.filter_var($oneFilterTestString,FILTER_VALIDATE_URL).']<br>';
    }else{
        echo 
'false!!!<br>';
    }
}

結果は

[ftp://ftp.is.co.za.example.org/rfc/rfc1808.txt]=>
[ftp://ftp.is.co.za.example.org/rfc/rfc1808.txt]
[gopher://spinaltap.micro.umn.example.edu/00/Weather/California/Los%20Angeles]=>
[gopher://spinaltap.micro.umn.example.edu/00/Weather/California/Los%20Angeles]
[http://www.math.uio.no.example.net/faq/compression-faq/part1.html]=>
[http://www.math.uio.no.example.net/faq/compression-faq/part1.html]
[mailto:mduerst@ifi.unizh.example.gov]=>
[mailto:mduerst@ifi.unizh.example.gov]
[news:comp.infosystems.www.servers.unix]=>
[news:comp.infosystems.www.servers.unix]
[telnet://melvyl.ucop.example.edu/]=>
[telnet://melvyl.ucop.example.edu/]
[http://www.ietf.org/rfc/rfc2396.txt]=>
[http://www.ietf.org/rfc/rfc2396.txt]
[ldap://[2001:db8::7]/c=GB?objectClass?one]=>
[ldap://[2001:db8::7]/c=GB?objectClass?one]
[mailto:John.Doe@example.com]=>
[mailto:John.Doe@example.com]
[news:comp.infosystems.www.servers.unix]=>
[news:comp.infosystems.www.servers.unix]
[tel:+1-816-555-1212]=>
false!!!
[telnet://192.0.2.16:80/]=>
[telnet://192.0.2.16:80/]
[urn:oasis:names:specification:docbook:dtd:xml:4.1.2]=>
false!!!
[http://aaaaaaaa.aa.aa]=>
[http://aaaaaaaa.aa.aa]
[http://aaaaaaaa.aa.aa/]=>
[http://aaaaaaaa.aa.aa/]
[callto:123-456-789]=>
false!!!
[callto:123-456-789/]=>
false!!!
[skype:111-222-444]=>
false!!!
[skype:123456789]=>
false!!!

つまり、
http://www.faqs.org/rfcs/rfc2396.html
のRFC に準拠していないスキーマはfalse になっちゃうみたいです。

それ以外は使えるみたいです。

メールアドレスは試していませんが、
これを使うと楽かもしれないな!と思いました。

一応メモでした
お粗末<_ _>

追伸:
因みにIPv6とIPv4 の例は
http://www.w3schools.com/php/filter_validate_ip.asp
にありました!

拍手[0回]

PR


  • 2014/10/14 12:29

コメント一覧

  • お名前
  • Email

  • コメント

  • Vodafone絵文字 i-mode絵文字 Ezweb絵文字
  • パスワード
[PR]