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:   

我想在“最近的交易价格:~”之后得到这个数字,这个数字会在 1-7 位之间变化,所以它需要与所有数字一起工作。我不确定如何正确解析字符串,所以我只得到“最近交易价格”之后的数字

我试过使用:

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

但这给了我之后的一切,所以我想我已经成功了。有人可以帮忙吗!

4

3 回答 3

3

尝试正则表达式:

preg_match("/Recent Trade Price:[^0-9]*([0-9]+)/",$yourtext,$matches);
$price=$matches[1];

正则表达式首先搜索以“最近交易价格:”开头的内容。然后它将搜索所有不是数字的字符。然后它将匹配以下所有数字。这些包含在 () 中,因为它们需要进入 $matches 数组。$matches[0] 是 RegExp 匹配的整个字符串,[1] 等是括号中的部分。所以这永远不会失败,只要你在输入中有一个最近的交易价格,即使 ~ 丢失或任何其他字符在数字之前或之后。

于 2013-06-26T16:11:02.970 回答
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 回答
0

之后你可以再做一次爆炸并得到第一个单词,如下所示:

$words = explode("Recent Trade Price:", $text);
$price = explode(" ",$words[1]);
echo $price[0];
于 2013-06-26T16:09:18.037 回答