代码之家  ›  专栏  ›  技术社区  ›  michaelmcgurk

从文本文档中获取数据

php
  •  0
  • michaelmcgurk  · 技术社区  · 8 年前

    我正试图从文本文件的一行中获取数据。

    我正在查找管道之后的值和后续数据。

    userlist.txt :

    micky.mcgurk@test.co|Test
    michelle.mcgurk@test.co|Test2
    

    PHP:

    <?php
    $user = "micky.mcgurk";
    $file = "userlist.txt";
    $search_for = $user;
    $contents = file_get_contents($file);
    $pattern = sprintf('/\b%s@([^|\s]+)\|/m', preg_quote($search_for));
    
    if (preg_match_all($pattern, $contents, $matches)) {
        echo implode("\n", $matches[1]);
        $resultat = substr(strrchr($contents, '|'), 1);
        echo $resultat;
    } else {
       echo "No user found";
    }
    

    $resultat 应平等 Test 不管我得到什么 Test2 .

    3 回复  |  直到 8 年前
        1
  •  -1
  •   SiL3NC3    8 年前

    也在工作…

    function startsWith($haystack, $needle)
    {
         $length = strlen($needle);
         return (substr($haystack, 0, $length) === $needle);
    }
    
    $contents = "micky.mcgurk@test.co|Test\n\r\michelle.mcgurk@test.co|Test2";
    $user = "micky.mcgurk";
    
    $contentLines = explode(PHP_EOL, $contents);
    $userExists = False;
    $result;
    
    foreach ($contentLines as &$line) {
        if (startsWith($line, $user))
        {
            $userExists = True;
            echo explode("|",$line)[1];
        }
    }
    
        2
  •  3
  •   Praveen Kumar Purushothaman    8 年前

    如果分割字符串而不是使用regexp,则会更容易。

    <?php
      $user = "micky.mcgurk";
      $file = "userlist.txt";
      $search_for = $user;    // Why so many? Redundant right? Why not remove this?
      $contents = file_get_contents($file);
      $lines = explode(PHP_EOL, $contents);
      $resultat = "";
    
      $found = false;
      foreach ($lines as $line) {
        $line = explode("|", $line);
        if ($user . "@test.co" == $line[0]) {
          $resultat = $line[1];
          echo $line[1];
        }
      }
      if ($resultat == "") {
        echo "User not found";
      }
    
        3
  •  0
  •   Bodo Hugo Barwich    8 年前

    正则表达式中只缺少一点细节。
    您正在查找此正则表达式:

    $pattern = sprintf('/%s@[^|]+\|(.*)$/m', preg_quote($search_for));
    

    你要找的内容将在 $matches[1][0] .

    我只是把你的脚本改了一点,让你想象一下搜索的不同步骤:

    <?php
    $user = "micky.mcgurk";
    $file = "userlist.txt";
    $search_for = $user;
    $contents = file_get_contents($file);
    $pattern = sprintf('/%s@[^|]+\|(.*)$/m', preg_quote($search_for));
    
    echo "ptn: '$pattern'\n";
    
    if (preg_match_all($pattern, $contents, $matches)) {
        echo "mtch: '" .  print_r( $matches, true) . "'\n";
        $resultat = $matches[1][0];
        echo "res: '$resultat'\n";
    } else {
       echo "No user found";
    }
    
    ?>
    

    所以它产生了这个输出:

    $ php userlist.php
    ptn: '/micky\.mcgurk@[^|]+\|(.*)$/m'
    mtch: 'Array
    (
        [0] => Array
            (
                [0] => micky.mcgurk@test.co|Test
            )
    
        [1] => Array
            (
                [0] => Test
            )
    
    )
    '
    res: 'Test'
    
    推荐文章