1

I have a txt file with lines in this format

this is a name|this is a type|this is a description
this is a name|this is a type|this is a description
this is a name|this is a type|this is a description

I need to access those lines and echo them like this:

<li type="this is a type" description="this is a description">this is a name</li>

I have no idea how to approach this.

Thanks in advance

Rasmus Nørskov
  • 478
  • 1
  • 6
  • 20

3 Answers3

2

Normally I wouldn't write code for you without you having provided an example of what you've tried, but in this case it's pretty basic.

Use PHP's file function to read a file into an array (line by line), then use explode to break that line up:

<?php

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

foreach($contents as $eachline) {
    list($name, $type, $description) = explode("|", $eachline);
    echo '<li type="' . $type . '" description="' . $description . '">' . $name . '</li>';
}

?>

PHP manual: http://us2.php.net/manual/en/function.file.php

scrowler
  • 24,273
  • 9
  • 60
  • 92
1

The first step is to read every line of the file.

How to read a file line by line in php

After that explode the string by the pipe symbol $out = explode("|", $string);

after that you have an array and you can access the values with $out[0]; for example.

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

This is easypeasy:

$parts = explode("|", $line);
$out = "<li type='$parts[1]' description='$parts[2]'>$parts[0]</li>"
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66