如何比较preg_match输出?

时间:2016-03-30 16:41:41

标签: php preg-match

所以我要做的是从txt文档addresses.txt中读取,看起来像这样

[9677]  1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi  balance= 5000000  
[9681]  1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq  balance= 10673000

并采用简单的数字" 9677"和" 9681"并且比较它们,我得到的是print_r输出这个数字,我无法比较它们。 PHP代码如下:

<?php
$lines = file("addresses.txt");

foreach ($lines as $line_num => $line) {
    $regex = '~\[(.*?)\]\s*~';

    preg_match($regex , $line, $match);

    print_r($match[1]);
    echo "<br>";
}
?>

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

 $Adresses = file_get_contents("addresses.txt");

 Preg_match_all("/\[(\d+)\]\s.*/", $Adresses, $Match);
 var_dump($Match);

 for ($x=0;$x<=count($Match[1])-2;$x++){
     if(($Match[1][$x]-$Match[1][$x+1]) >=20 || ($Match[1][$x+1]-$Match[1][$x]) >=20){
         Echo $Match[0][$x] . "<br>";
         Echo $Match[0][$x+1] . "<br><br>";
      }
  }

输入:

[9677]  1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi  balance= 5000000
[9681]  1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq  balance= 10673000
[9977]  1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi  balance= 5000000
[9681]  1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq  balance= 10673000
[9127]  1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi  balance= 5000000
[9121]  1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq  balance= 10673000

输出:

[9681] 1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq balance= 10673000 
[9977] 1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi balance= 5000000 

[9977] 1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi balance= 5000000 
[9681] 1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq balance= 10673000 

[9681] 1C9BXAXYjb6W12RSmrpsrAUYPBB7n59ssq balance= 10673000 
[9127] 1F8gYXp2EogDAymQYu4eFQpCxRF3kz6GJi balance= 5000000