1

いくつかのIPアドレスの情報を提供するスクリプトがあります。

テキストから国を抽出したいと思います。

次のテキストでは、国の行は"Country: US"

表示したい: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:  arin-contact@google.com
OrgTechRef:    http://whois.arin.net/rest/poc/ZG39-ARIN

OrgAbuseHandle: ZG39-ARIN
OrgAbuseName:   Google Inc
OrgAbusePhone:  +1-650-253-0000 
OrgAbuseEmail:  arin-contact@google.com
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
#
4

5 に答える 5

2

必要な正規表現だけの場合は、これを試してください。国IDは最初のグループに含まれます。

Country:\s*([A-Z]{2})
  • Country:-リテラルに一致
  • \s*-任意の数の空白、タブなどに一致します。
  • ([A-Z]{2})-任意の文字(大文字)を2回一致させてキャプチャします

preg_match_allこのパターンのすべての出現が必要な場合に使用します

于 2012-04-28T13:01:35.817 に答える
2

preg_matchを使用すると、次のようなことができます。

if (preg_match('/^Country:\s*([A-Z]{2,3)$/m', $str, $match)) {
    echo $match[1];
}
于 2012-04-28T13:04:35.810 に答える
1

whoisデータを操作するためのphpwhoisライブラリがあります。応答を配列として取得します。

于 2012-04-28T13:05:47.337 に答える
0

preg_matchで抽出

preg_match("/Country:(.*)\"/siU", $str, $match);
echo trim($match[1]);
于 2012-04-28T15:46:48.757 に答える
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);

------------------------------------
配列([0] =>国:FR)

于 2016-01-31T18:16:57.477 に答える