PHP从文件中获取分数

时间:2018-02-21 20:35:17

标签: php preg-match-all

我有一个htm文件,可以保存游戏的分数。我需要找到高于65的每个分数,将这些分数和名称仅插入到数据库中,然后在页面上打印整个文件,其中高分线为红色。 htm文件包含简单文本:

<pre>
Thursday Afternoon Pairs Thursday Aft Session February 1, 2018

Scores after  5 rounds  Average:   50.0      Section  A  North-South

Pair    Pct   Score      Section Rank      MPs     

                         A     B     C  

  5   65.00   65.00  A   1                0.50(A)  Joseph - Marlene

  1   47.00   47.00  B  2/3    1          0.30(A)  Janet - Mina

  3   47.00   47.00  A  2/3               0.30(A)  Renee - Reichle
</pre>

文件中没有其他标签,文件也无法修改。

我只是试图找到高分,并看看我是否可以打印那些。这不会返回每次都找到的匹配项。

$file = 108911.htm;
$pct = "([1-9][0-9]\.[0-9]{2})";
$highgame_pct = 65; 

$contents = file_get_contents($file);

$pattern = preg_quote($pct, '/');

$pattern = "/^.*$pct.*\$/m";

if(preg_match_all($pattern, $contents, $matches) >= $highgame_pct){
   echo "Found matches:<br />";
   echo implode("<br />\n", $matches[0]);
}
else{
   echo "No matches found";
}

2 个答案:

答案 0 :(得分:0)

像任何事情一样,有很多方法可以做到这一点,这是一个尝试:

AddRef()

给你:

Release()

答案 1 :(得分:0)

preg_match_all 返回完整模式匹配的数量(可能为零),如果发生错误则返回FALSE。

使用类似的正则表达式模式并使用回调函数来过滤数组:

# define array filter callback function
function filterArray($value1){
    return ($value1 >= 65.00);
}


# read file 
$file = "108911.htm";
$contents = file_get_contents($file);

# define regex
$pattern = "/(\d{2}\.\d{2})/m";

# find matches 
$val = preg_match_all($pattern, $contents, $matches);
echo "Count of matches = $val\n";

# filter matches array 
$filteredArray = array_filter($matches[0], 'filterArray');

# print matched element
if (count($filteredArray) > 0) {
   echo "Found matches bigger or equal to 65.00:<br />";
   echo implode("<br />\n", $filteredArray);
}
else{
   echo "No matches found";
}

输出:

Count of matches = 10
Found matches bigger or equal to 65.00:<br />
65.00<br />
65.00

你仍然需要修改它以匹配&#34;分数&#34;仅限列。