【问题标题】:file_get_contents + preg_match > find a line starting with a wordfile_get_contents + preg_match > 查找以单词开头的行
【发布时间】:2014-05-05 15:11:32
【问题描述】:

我执行file_get_contents 来获取一个长文件,我想找到以“MyParam”开头的行(只有一个)(例如)。 我不想逐行剪切文件,我只想使用preg_match(或等效项)来完成。 我尝试了很多东西,但没有成功。 提前致谢

文件内容

//some text
//some text
//MyParam "red"
//some text
MyParam "blue"
Some text
// Some text

我只想得到MyParam "blue"

【问题讨论】:

  • 请显示您尝试过的内容,也欢迎提供输入示例
  • 问题已部分理解...您能发布您的输入和预期输出吗?
  • 预匹配比逐行切割要慢

标签: php regex preg-match file-get-contents


【解决方案1】:

如果你必须使用正则表达式,你可以这样做:

preg_match('/^MyParam[^\r\n]*/m', $text, $matches);
var_dump($matches[0]);

解释:

  • [^\r\n] - 匹配除\r\n 之外的任何字符的字符类。
  • m - multiline modifier,这将 ^ 的含义从“assert position at the beginning of the string”更改为“assert position at the 每行"的开头。

这里,$matches[0] 将包含完整匹配的字符串(在这种情况下,这就是您想要的)。

输出:

string(14) "MyParam "blue""

Demo

【讨论】:

  • @HamZa:那不也包括换行符吗?见这里:eval.in/146768
  • 发挥创意并使用[^\r\n]*:P
  • 它有效。谢谢。 preg_match真的很慢,还是那样做没关系?
  • @Peter:我想说这取决于你的字符串的长度。如果字符串很长,我会使用 Casmir 的解决方案,因为它逐行读取。在这种情况下,它应该无关紧要。
  • @AmalMurali 该文件大约有 1000 行。
【解决方案2】:

在这里使用 file_get_contents 不是最佳选择,因为您可以逐行读取文件

$handle = fopen("yourfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (strpos($line, 'MyParam')===0) {
            echo 'found';
            break;
        }
    }
} else {
    echo 'error opening the file';
} 
fclose($handle);

注意:如果需要在引号之间提取参数值,可以使用:

$value = explode('"', $line, 3)[1];

【讨论】:

    【解决方案3】:

    您可以使用 strpos() 来查找字符串中子字符串第一次出现的位置。

    http://nl1.php.net/strpos

    【讨论】:

      【解决方案4】:

      不需要正则表达式:

      $pos = strpos($filecontent, "\nMyParam");
      $endline = strpos($filecontent, "\n", $pos + 1);
      $line = substr($filecontent, $pos + 1, $endline - $pos - 1) ;
      

      未测试,字符串中可能有 +-1 的偏移量,但它给了你想法。

      编辑:如果您确定“MyParam”没有出现在文件的其他位置,您可以删除第一个“\n”。

      【讨论】:

        【解决方案5】:

        我会在这里考虑几种不同的方法。要考虑的最重要的事情是您不需要通过file_get_contents() 将整个文件读入内存。我倾向于只使用 shell 命令来执行此操作:

        $file = '/path/to/file';
        $search_pattern = '^MyParam';
        $file_safe = escapeshellarg($file);
        $pattern_safe = escapeshellarg($search_pattern);
        $result = exec('grep ' . $pattern_safe . ' ' . $file_safe);
        

        或者,您可以一次读取一行文件以查找匹配项:

        $result = '';
        $file = '/path/to/file';
        $search = 'MyParam'; // note I am not using regex here as this is unnecessary in your case
        $handle = fopen($file, 'r');
        if ($handle) {
            while($line = fgets($handle) !== false) {
                if (strpos($line, $search) === 0) { // exact match is important here
                    $result = $line;
                    break;
                }
            }
            fclose($handle);
        }
        

        【讨论】:

          猜你喜欢
          • 2011-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-06
          • 1970-01-01
          • 2014-07-06
          • 2015-07-14
          相关资源
          最近更新 更多