0

PHP にこの未加工のテキスト ファイルがあり、特定の番号を取得したいと考えています。テキストファイルは次のとおりです。

Bones - Price Guide and Exchange - 2007 Old School RuneScape Jump 
to content Search Advanced Search section: ForumsMembersHelp Files 
Calendar   View New Content      Sign In    Create Account Zybez 
Runescape Help Forums Members OSRS Price Guide 2007 RS Help Premium 
More Runescape Community → OSRS Price Guide → Bones Price Guide: Bones 
The charts and price calculations are currently undergoing a transition. 
Prices may be inaccurate until this is completed, especially for 
less-active items. Please be patient, and notify us of any major problems. 
Recent Trade Price: ~55 GP (rising) Average Sell Offer 50 GP Average Buy 
Offer 60 GP High Alchemy Value 1 GP View more Bones info on 2007rshelp.com 
All Time 90 Days 30 Days 7 Days Latest Offers Offer RS Name Time Info 
Selling 2,000 for 70 GP ea maulsurba11s 5 hours ago pm Buying 25 for 200
 GP ea placation 15 hours ago pm me soon Selling 500 for 60 GP ea iharleyquinn
 18 hours ago Buying 30 for 60 GP ea purple-mlk 18 hours ago Buying 500 for 
60 GP ea og-real-og 24 hours ago Buying 25 for 200 GP ea haxore 32 hours ago
 Buying 25 for 75 GP ea attack-hd 41 hours ago pm me pls ;) Buying 1,000 for
 75 GP ea pupz45 43 hours ago Buying 100 for 200 GP ea i3-a-l-l-e-n 44 hours
 ago Look up a price Post your offer I am BuyingSelling for GP ea. My RS Name
 is Contact me by PMCC Scam Alert Never meet a player in Brimhaven or Entrana. 
Anyone who asks is trying to steal your items. Helpful Links 2007 RuneScape 
Help & Info Bank and Inventory Sales Money Making Tips Join a RuneScape Clan
 Most-Traded Items Nature rune 212 Blue h'ween mask 935,095 Red h'ween mask 
1,135,278 Bow string 88 Yew logs 485 Abyssal whip 1,761,182 Green h'ween mask
 695,655 Yew longbow 740 Prayer potion (3) 6,630 Halloween mask set 2,855,149
 Get Help | API | All Items Created by the almighty No1 1000. Runescape 
Community → OSRS Price Guide → Bones Privacy Policy Change Theme     
IP.Board Graphite (3.3) Thoreau Dark (3.3) Thoreau (3.3) Mark Community 
Read Forums Members Mark all as read Help Community Forum Software by 
IP.Board Licensed to: Zybez Corporation Curse Curse is the #1 Resource 
for core online gamers. Not a Member? Get your Free Account! Sign up for 
Free! Featured SitesMore Guild Wars 2 Guru Guild Wars 2 Guru The latest 
and greatest on Tyria. LoL Pro LoL Pro Dominate with Pro LoL guides. 
MMO-Champion MMO-Champion Keep ahead with the champions of WoW coverage. 
GW2DB GW2DB Explore Tyria with Curse and GW2DB. Browse Core Curse MMO-Champion
 WowStead CurseForge WowAce SkyrimForge SC2Mapster Bukkit Forums LoLPro ExilePro
 Community Minecraft Forum Terraria Online Arena Junkies Guild Wars 2 Guru 
DiabloFans FPS General DarthHater Defiance Forum Wildstar Forums Database
 Guild Wars 2 DB Zybez DarthHater DB Aion Armory WoW Database Marriland Wiki
 Minecraft Wiki Terraria Wiki Wowpedia Skyrim Wiki Wiki SWTOR Dragon Nest Wiki
 Vindictus Wiki Back to Top About Us Advertising Privacy Policy Terms of Service
 Premium Terms of Service Curse Newsletter Jobs at Curse Handcrafted in San Francisco
 & Huntsville Font: Arial | Calibri | Lucida Console | Verdana  
 Font Size: 9px | 10px | 11px | 12px | 10pt | 12pt   Color:   

「Recent Trade Price:~」の後の数字を取得したいのですが、数字は 1 ~ 7 桁の範囲で変化するため、すべての数字で機能する必要があります。文字列を正しく解析する方法がわからないので、「最近の取引価格」の後に数字だけを取得します

私は使用してみました:

$text = $html->plaintext;
$words = explode("Recent Trade Price:", $text);
echo $words[1];

しかし、これですべてが得られたので、途中までだと思います。誰か助けてください!

4

3 に答える 3

2

これは、このテキストがどの程度厳格で構造化されているかによって異なります。数字の前に常にスペースと「~」がありますか?

ここでは、おそらく正規表現が最も役立ちます。 preg_matchこれであなたを助けます。

http://php.net/manual/en/function.preg-match.php

preg_match('/Recent Trade Price: ~(\d+)/', $text, $matches);
print_r($matches);

印刷します:

Array ( [0] => Recent Trade Price: ~55 [1] => 55 )

編集

あなたのコメントによると、取得しようとしている数字にコンマが含まれている場合は、最初のスペースまでのすべての文字を取得する別の正規表現を使用できます

preg_match('/Recent Trade Price: ~(\S+)/', $text, $matches);
print_r($matches);

印刷します:

Array ( [0] => Recent Trade Price: ~555,000 [1] => 555,000 )

そこから、必要に応じてカンマを取り除くことができます。繰り返しますが、これはテキスト形式がどれだけ厳格で予測可能かによって異なります...

于 2013-06-26T16:13:19.473 に答える