2012-04-28 23 views
1

私はいくつかのIPアドレスの情報を提供するスクリプトを持っています。テキストから何かを抽出するには?

テキストから国を抽出します。 USのみ

テキストは次のとおりです:次のテキストカントリーラインで

は、私が表示したい"Country: US"

ある - これを試してみてください

[Querying whois.arin.net] 
[whois.arin.net] 
# 
# Query terms are ambiguous. The query is assumed to be: 
#  "n 173.194.74.100" 
# 
# Use "?" to get help. 
# 

# 
# The following results may also be obtained via: 
# http://whois.arin.net/rest/nets;q=173.194.74.100?showDetails=true&showARIN=false&ext=netref2 
# 

NetRange:  173.194.0.0 - 173.194.255.255 
CIDR:   173.194.0.0/16 
OriginAS:  AS15169 
NetName:  GOOGLE 
NetHandle:  NET-173-194-0-0-1 
Parent:   NET-173-0-0-0-0 
NetType:  Direct Allocation 
RegDate:  2009-08-17 
Updated:  2012-02-24 
Ref:   http://whois.arin.net/rest/net/NET-173-194-0-0-1 


OrgName:  Google Inc. 
OrgId:   GOGL 
Address:  1600 Amphitheatre Parkway 
City:   Mountain View 
StateProv:  CA 
PostalCode:  94043 
Country:  US 
RegDate:  2000-03-30 
Updated:  2011-09-24 
Ref:   http://whois.arin.net/rest/org/GOGL 

OrgTechHandle: ZG39-ARIN 
OrgTechName: Google Inc 
OrgTechPhone: +1-650-253-0000 
OrgTechEmail: [email protected] 
OrgTechRef: http://whois.arin.net/rest/poc/ZG39-ARIN 

OrgAbuseHandle: ZG39-ARIN 
OrgAbuseName: Google Inc 
OrgAbusePhone: +1-650-253-0000 
OrgAbuseEmail: [email protected] 
OrgAbuseRef: http://whois.arin.net/rest/poc/ZG39-ARIN 

# 
# ARIN WHOIS data and services are subject to the Terms of Use 
# available at: https://www.arin.net/whois_tou.html 
# 
+0

セパレータ ":"を使用して文字列を分割しようとしましたが、foreach - >次にstrposを使用しました – semsem

+0

私はIPアドレスの国を取得したいIPアドレスapi – semsem

+0

を "\ n"文字で分割してからforeach - > splitで提供するサイトから取得する代わりに、exec( 'whois $ ip') ":" –

答えて

2

それはあなたが必要なだけの正規表現だ場合 - 国IDは最初のグループになります

Country:\s*([A-Z]{2}) 
  • Country: - 試合リテラル
  • \s* - 一致し、こののすべての出現が必要な場合は任意の文字(大文字)を2回

使用preg_match_allをキャプチャ - など

  • ([A-Z]{2})を空白の任意の数、タブと一致パターンするpreg_matchで

  • +0

    私はあなたのコードを試しましたが、このipの結果は になりますが、別のipを試してみると空の結果になります – semsem

    +1

    @semsem - どういう意味ですか? preg_match_allを試しましたか? http://php.net/manual/en/function.preg-match-all.php –

    +0

    @semsem - これは他の例です。 –

    2

    あなたのような何かを行うことができます。

    if (preg_match('/^Country:\s*([A-Z]{2,3)$/m', $str, $match)) { 
        echo $match[1]; 
    } 
    
    +0

    これは私にこのIPだけの結果を与えますが、別の国を試してみると国と​​一致しません – semsem

    +1

    whoisの国:...部分を投稿できますか? – smrtl

    +0

    私は修飾子/ iを追加してこの問題を解決しました: 助けてくれてありがとう – semsem

    1

    whoisデータの操作にはphpwhoisライブラリがあります。それは配列としての応答を得るでしょう。するpreg_match

    preg_match("/Country:(.*)\"/siU", $str, $match); 
    echo trim($match[1]); 
    
    +0

    このライブラリを提供していただきありがとうございます 私はそれを試してみます – semsem

    0

    エキス--------------------------------- ---
    アレイ([0] =>国:FR)

    0
    $regex = "/country:[\ \t\r\n\f][A-Z]+\s/"; 
    
    $txt = "descr: NCC#200X44704917 
    country: FR 
    admin-c: ACPSA223-RIPE 
    tech-c: TCWQQP8-RIPE"; 
    
    preg_match($regex, $txt, $result); 
    
    print_r($result); 
    

    関連する問題