如何使用PHP仅从文件中获取特定内容。

如何使用PHP仅从文件中获取特定内容。,php,Php,如何使用PHP仅从文件中获取特定内容 我有一个包含以下内容的文件: reference 1.pdb mobile 4r_1.pdb ignore fit mobile 4r_10.pdb ignore fit mobile 4r_22220.pdb ignore fit 现在,我想获取所有名称,即(输出) 在一个数组中并打印它 我用php编写的程序运行不正常,可以看一下 $data = file_get_contents('file.txt'); // to read the file $co

如何使用PHP仅从文件中获取特定内容

我有一个包含以下内容的文件:

reference 1.pdb
mobile 4r_1.pdb
ignore
fit
mobile 4r_10.pdb
ignore
fit
mobile 4r_22220.pdb
ignore
fit
现在,我想获取所有名称,即(输出)

在一个数组中并打印它

我用php编写的程序运行不正常,可以看一下

$data = file_get_contents('file.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
$output4 = preg_grep("/mobile/i",$convert); //take only the line starts with mobile and put it in an array
if ($output4 !="/mobile/i")
{ 
print $output4;
print "\n";
}

请帮忙!要仅提取名称,请使用以下代码:

$data = file_get_contents('file.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
$output4 = preg_grep("/mobile/i",$convert);
if (count($output4))
{ 
   foreach ($output as $line) {

      print $line; // or substr($line, 6) to remove mobile from output
      print "\n";
   }
}
注意:

而不是做

$data = file_get_contents('file.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
您可以使用以下函数将文件读入数组:

$convert = file('file.txt'); // to read the file

下面的代码应该可以工作:

$data = file_get_contents('file.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
$output4 = preg_grep("/mobile/i",$convert);
if (count($output4))
{ 
   foreach ($output as $line) {

      print $line; // or substr($line, 6) to remove mobile from output
      print "\n";
   }
}
注意:

而不是做

$data = file_get_contents('file.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
您可以使用以下函数将文件读入数组:

$convert = file('file.txt'); // to read the file

preg_grep返回一个匹配行数组,您的条件是将$output4视为字符串

在数组上循环以打印出每一行,并使用substr或str_replace从字符串中删除不需要的字符

$data = file_get_contents('test.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
$output4 = preg_grep("/mobile/i",$convert); //take only the line starts with mobile and put it in an array
foreach($output4 as $entry) {
    print str_replace("mobile ", "", $entry) . "\n";
}

preg_grep返回一个匹配行数组,您的条件是将$output4视为字符串

在数组上循环以打印出每一行,并使用substr或str_replace从字符串中删除不需要的字符

$data = file_get_contents('test.txt'); // to read the file
$convert = explode("\n", $data); // take it in an array
$output4 = preg_grep("/mobile/i",$convert); //take only the line starts with mobile and put it in an array
foreach($output4 as $entry) {
    print str_replace("mobile ", "", $entry) . "\n";
}
试试这个:

$convert = explode("\n", $data); // take it in an array
$filenames = array();


foreach ($convert as $item) {
    if(strstr($item,'mobile')) {
        array_push($filenames,preg_replace('/mobile[\s]?([A-Za-z0-9_]*).pdb/','${1}',$item));
    }
}
$content = file_get_contents('file.txt');
$lines = explode("\n", $content);
foreach ($lines as $line) {
    if (preg_match('/^mobile\s+(.+)$/', $line, $match)) {
        echo $match[1], "\n";
    }
}
现在所有文件名(假设它们是文件名)都在数组中
$filenames

尝试以下操作:

$convert = explode("\n", $data); // take it in an array
$filenames = array();


foreach ($convert as $item) {
    if(strstr($item,'mobile')) {
        array_push($filenames,preg_replace('/mobile[\s]?([A-Za-z0-9_]*).pdb/','${1}',$item));
    }
}
$content = file_get_contents('file.txt');
$lines = explode("\n", $content);
foreach ($lines as $line) {
    if (preg_match('/^mobile\s+(.+)$/', $line, $match)) {
        echo $match[1], "\n";
    }
}
现在所有文件名(假设它们是文件名)都在数组中
$filenames

尝试以下操作:

$convert = explode("\n", $data); // take it in an array
$filenames = array();


foreach ($convert as $item) {
    if(strstr($item,'mobile')) {
        array_push($filenames,preg_replace('/mobile[\s]?([A-Za-z0-9_]*).pdb/','${1}',$item));
    }
}
$content = file_get_contents('file.txt');
$lines = explode("\n", $content);
foreach ($lines as $line) {
    if (preg_match('/^mobile\s+(.+)$/', $line, $match)) {
        echo $match[1], "\n";
    }
}
试试这个:

$convert = explode("\n", $data); // take it in an array
$filenames = array();


foreach ($convert as $item) {
    if(strstr($item,'mobile')) {
        array_push($filenames,preg_replace('/mobile[\s]?([A-Za-z0-9_]*).pdb/','${1}',$item));
    }
}
$content = file_get_contents('file.txt');
$lines = explode("\n", $content);
foreach ($lines as $line) {
    if (preg_match('/^mobile\s+(.+)$/', $line, $match)) {
        echo $match[1], "\n";
    }
}

尝试
var\u转储($output4)我可能会首先搜索包含
pdb
的行,然后只找到最右边的空格,并分解/保留最右边的值。查找
mobile
并在每行的右侧添加substr。尝试
var\u dump($output4)我可能会首先搜索包含
pdb
的行,然后只找到最右边的空格,并分解/保留最右边的值。查找
mobile
并在每行的右侧添加子项。非常感谢:)没问题。你也许可以在这个正则表达式上做一些工作。现在它非常特定于您发布的内容。但是当我打印数组时,它以4r_1、4r_1、4r_10的方式打印,其中$filenames[0]=>4r_1和$filenames[1]=>4r_1、4r_10。但是我想要$filenames[0]=>4r\u 1和$filenames[1]=>4r\u 10:(@user1971853,做一个
var\u dump($filenames)
。我敢打赌这个数组很好,你迭代的方式也不好。完全相同的代码显示它可以正常工作。非常感谢:)没问题。你也许可以在这个正则表达式上做一些工作。现在它非常特定于您发布的内容。但是当我打印数组时,它以4r_1、4r_1、4r_10的方式打印,其中$filenames[0]=>4r_1和$filenames[1]=>4r_1、4r_10。但是我想要$filenames[0]=>4r\u 1和$filenames[1]=>4r\u 10:(@user1971853,执行
var\u dump($filenames)
。我敢打赌数组很好,而您迭代的方式也不好。使用完全相同的代码显示它按预期工作。