PHP Reading Fixed-Width Delimited Data – String Function

<?php
  $flatfile = "data.txt";  if (file_exists ($flatfile)){    $rows = file ($flatfile);
    for ($i = 0; $i < count ($rows); $i++){      $item = substr ($rows[$i],0,20);
      $amount = substr ($rows[$i],20,40);      echo "Item: " . rtrim ($item) . " has " . rtrim ($amount) . " unit (s) left.<br />";
    }
  } else {
    echo "File does not exist.";
  }?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.