解析数据源

发布于 2024-07-06 20:09:21 字数 207 浏览 15 评论 0原文

我不是 PHP 方面最好的人,如果有人能提供帮助,我将非常感激。 基本上,我需要解析数据源的每一行,并获取每个“|”之间的每一位信息。 - 然后我可以将其添加到数据库中。 我认为我可以使用爆炸来处理从“|”之间获取信息,但我需要一些帮助来将文本文件中的每一行解析为单数。 事实上,为了让它变得更简单,我只需要它使用变量的每一行,我将使用文本区域和表单将内容提交到变量。 任何帮助将不胜感激!

I'm not the best at PHP and would be extremely grateful if somebody could help. Basically I need to parse each line of a datafeed and just get each bit of information between each "|" - then I can add it to a database. I think I can handle getting the information from between the "|"'s by using explode but I need a bit of help with parsing each line from a text file as a singular. Infact to make it even more simple, I just need it to use each line of a variable, I will submit content to the variable using a textarea and a form. Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安穩 2024-07-13 20:09:21

您可以将文件读入行数组并执行所有拆分:

$lines = file("filename");
foreach($lines as $line) {
    $parts = explode("|", $line);
    // do the database inserts here
}

如果您已经将所有文本存储在变量中(如您所说)(例如,使用类似 file_get_contents() 的内容),您可以先在 \n​​ 上爆炸,然后再爆炸执行与上面相同的 foreach 语句。

You can read a file into an array of lines and do all the splitting with:

$lines = file("filename");
foreach($lines as $line) {
    $parts = explode("|", $line);
    // do the database inserts here
}

If you already have all the text in a variable as you said (e.g., with something like file_get_contents() ), you can explode on \n first and then do the same foreach statement as above.

夏末染殇 2024-07-13 20:09:21

如果您正在阅读文本区域帖子,则可以使用爆炸函数,使用换行符作为分隔符,将变量中的每个“行”作为数组的新元素,然后您可以对数组元素进行爆炸。

$sometext = "balh | balh blah| more blah \n extra balh |some blah |this blah";

$lines = explode("\n", $sometext);
foreach($lines as $oneLine)
{
    $lineElements[] = explode("|", $oneLine);
}

你有一个 elems 的二维数组。

如果您正在读取文件,则可以简单地使用此处记录的文件函数:

https://www.php.net/manual/en/function.file.php

将文件的每一行作为数组的元素。

If you are reading out of your textarea post, you can use the explode function using the newline character as your separator to get each "line" in the variable as a new element of an array, then you can do explode on your array elements.

i.e.

$sometext = "balh | balh blah| more blah \n extra balh |some blah |this blah";

$lines = explode("\n", $sometext);
foreach($lines as $oneLine)
{
    $lineElements[] = explode("|", $oneLine);
}

then you have a 2d array of your elems.

If you are reading out of a file, you can simply use the file function documented here:

https://www.php.net/manual/en/function.file.php

to get each line of the file as an element of an array.

半边脸i 2024-07-13 20:09:21

有一个现成的 PHP 解析库可以自动检测 CSV 格式。

示例:

$reader = new Dfp_Datafeed_File_Reader();
$reader->setLocation('test.csv');

foreach ($reader AS $record) {
    print_r($record);
}

可以在此处下载,并且有一些文档此处

There is a ready-built PHP parsing library that can auto-detect the CSV format.

Example:

$reader = new Dfp_Datafeed_File_Reader();
$reader->setLocation('test.csv');

foreach ($reader AS $record) {
    print_r($record);
}

It's available to download here, and there's some documentation here.

Oo萌小芽oO 2024-07-13 20:09:21

如果文件较小,可以使用file()将其读入数组,一行每个元素。

如果失败,请使用 fgets() 循环读取文件

$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);
   echo $buffer;
}
fclose($handle);

If the file is small, you can use file() to read it into an array, one line per element.

Failing that, read the file in loop using fgets()

$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);
   echo $buffer;
}
fclose($handle);
烟织青萝梦 2024-07-13 20:09:21

您可以使用爆炸来获得两者:

$myFile = "File.txt";
$fh = fopen($myFile, 'r');
$data = fread($fh);
fclose($fh);
$newLines = explode("\n",$data);

foreach($newLines as $s)
{
  $parsed = explode("|",$s);
  foreach($parsed as $item)
  {
    // do your db load here
  }
}

You can use explode to get both:

$myFile = "File.txt";
$fh = fopen($myFile, 'r');
$data = fread($fh);
fclose($fh);
$newLines = explode("\n",$data);

foreach($newLines as $s)
{
  $parsed = explode("|",$s);
  foreach($parsed as $item)
  {
    // do your db load here
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文