Php 如何读取包含200行文本的文本文件,并识别相同的字符串值?
我有一个包含200行文本的文本文件。如何使用PHP比较每行文本并识别相同的值。这可能吗? 我可以将这些行转换为XML格式吗?Php 如何读取包含200行文本的文本文件,并识别相同的字符串值?,php,Php,我有一个包含200行文本的文本文件。如何使用PHP比较每行文本并识别相同的值。这可能吗? 我可以将这些行转换为XML格式吗? 我可以将文本文件转换为HTML并将重复值附加到未排序的列表中吗?是否要创建重复行数组?如果是这样,您可以使用以下内容: <?php /* load the file as an array of lines */ $lines = file('myfile'); /* put them in sorted order */ sort($lines); /* l
我可以将文本文件转换为HTML并将重复值附加到未排序的列表中吗?是否要创建重复行数组?如果是这样,您可以使用以下内容:
<?php
/* load the file as an array of lines */
$lines = file('myfile');
/* put them in sorted order */
sort($lines);
/* loop over each line comparing it to the previous, to determine if it is a dup */
$last_line = null;
$duplicates = array();
foreach ($lines as $line)
{
/* only add it to the duplicates array if it is not already there */
if (strcmp($line, $last_line) == 0 && ! in_array($line, $duplicates))
{
$duplicates[] = $line;
}
$last_line = $line;
}
/* view the duplicates */
var_dump($duplicates);
?>
我先回答你一个问题:是的,你可以。我累了,所以我要告诉你们的只是生产案例中凌乱和不可用的代码
您是在寻找重复的行还是在行内寻找重复的行?嗯,您更快了:/Your更好,我认为,尽管注意,如果您只是想从文件中消除重复项,您也可以使用array_unique。@RPM-您应该使用array_unique($lines);代替
排序
并处理它。@Brand Horsley-最终结果是NULL@RPM-如果您有新问题,应修改您的问题或发布新问题$行=数组\唯一($line);应该消除所有重复项。为什么要对行进行排序,然后在数组中使用?in_数组不知道它是为B搜索排序的,似乎浪费了cpu周期。
$lines = file("file.txt");
sort($lines);
foreach ($lines as $line)
{
if (in_array($line, $lines) && !in_array($line, $dupes))
{
$dupes[] = $line;
}
}
print_r($dupes);