pmerino
pmerino

Reputation: 6120

Parse an txt file with tags in PHP

I have a .txt file that is like this:

Title: Test
Author: zad0xsis
Date: July 13th, 2011
Body: This is a test post and this can continue until the file end

How could I make PHP to recognize the "tags" and make the content to a new string? Thanks in advance! :D

Upvotes: 2

Views: 1531

Answers (6)

Dereleased
Dereleased

Reputation: 10087

$fc = file('some_file.txt'); // read file into array
foreach ($fc as $line) {
    list($tag, $content) = explode(':', $line, 2);
    // do something here
}

Now, are there multiple unrelated sets in each file? If so, you'll have to look for some marker, maybe a new line, and do a reset. Hopefully you can figure this part out on your own.

Some functions for you to check out:

Edit: slightly expanding the example:

$fc = file('some_file.txt'); // read file into array
foreach ($fc as $index => $line) {
    list($tag, $content) = explode(':', $line, 2);
    // do something here
    if ('body' == strtolower($tag)) {
        $content = join(array_slice($fc, $index + 1, count($fc)));
        break;
    }
}

More functions for you!

  • strtolower
  • join (aka implode)
  • array_slice
  • trim - this is not used in my solution, but you may want to use it to trim the newline chars from the end of the lines as returned by file(). Alternatively, you can use the FILE_IGNORE_NEW_LINES flag when calling file(), and more information on that can be found in the PHP Manual entry for file() (also linked above).

Upvotes: 5

Patrick Desjardins
Patrick Desjardins

Reputation: 140803

<?php
$tagValue = array();
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");

while(!feof($file))
{
  $line = fgets($file);
  $tagDelimiter = strpos ($line ,":");
  $tag = substr($line,0,$tagDelimiter);
  $value = substr($line,$tagDelimiter+1,strlen($line)-$tagDelimiter);
  $tagValue[$tag] = $value;
}
fclose($file);
?>

You can access your data : $tagValue["Title"]

Upvotes: 2

ComFreek
ComFreek

Reputation: 29424

Use strpos() and substr():

function parse($filename)
{
  $lines = file($filename);
  $content = array();
  foreach ($lines as $line)
  {
    $posColon = strpos($line, ":");
    $tag = substr($line, 0, $posColon);
    $body = substr($line, $posColon+1);

    $content[$tag] = trim($body);
  }
  return $content;
}

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101604

Another solution: demo here

<?php

  //$sample = file_get_contents('myfile.txt'); // read from file

  $sample = "Title: Test
Author: zad0xsis
Date: July 13th, 2011
Body: This is a test post and this can continue until the file end";

  $re = '/^(?<tag>\w+):\s?(?<content>.*)$/m';

  $matches = null;
  if (preg_match_all($re, $sample, $matches))
  {
    for ($_ = 0; $_ < count($matches['tag']); $_++)
      printf("TAG: %s\r\nCONTENT: %s\r\n\r\n", $matches['tag'][$_], $matches['content'][$_]);
  }

produces:

TAG: Title
CONTENT: Test

TAG: Author
CONTENT: zad0xsis

TAG: Date
CONTENT: July 13th, 2011

TAG: Body
CONTENT: This is a test post and this can continue until the file end

Thought I'd use named tags just for GPs. Also, if need-be, you can replace the (?<tag>\w+) with something more vague such as (?<tag>.*?) if there could be spaces, numbers, etc.

Upvotes: 3

Wilson212
Wilson212

Reputation: 563

you can do this:

$file = file('file.txt');

foreach($file as $line)
{
    if(preg_match('/(.*) : (.*)/iUs', $line, $match)
    {
         $tag = $match[1];
         $value = $match[2]
    }
}

Upvotes: 1

genesis
genesis

Reputation: 50976

$file = file("file.txt");
foreach($file as $line)
{
    preg_match("|(.*?): (.*?)|", $line, $match);
    $tag = $match[1];
    $content = $match[2];
}

Upvotes: 2

Related Questions