正则表达式PHP preg_match()参数2应该是一个字符串错误

时间:2016-01-11 08:26:28

标签: php regex

$lines = file('datatest.txt');
$lines = preg_grep("/word/", $lines); //founds me "word"
//$lines = preg_replace("/\(([^\d]+)\)/", '',  $lines2); //removing brackets with text
preg_match('/"(.*?)"/', $lines, $result); //after this line I am getting an error

从文件输入:

Random (aaa) "word"

输出应如下所示:

word

据我所知,我有一个带字符串的文件数组,但为什么我会收到这个错误?

1 个答案:

答案 0 :(得分:2)

在线preg_match('/"(.*?)"/', $lines, $result);

$ lines不是字符串: preg_grep()返回由与给定模式匹配的输入数组元素组成的array

http://php.net/manual/en/function.preg-grep.php

preg_match()只接受string作为第二个参数。

http://php.net/manual/en/function.preg-match.php

<强>解决方案

如果您确定$ lines包含文件的行,您可以在for(每个)循环中逐个传递它们。