How to Parse a Tab-Delimited Text File Into an Array in PHP

Written by cathlene s. baptista
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Parse a Tab-Delimited Text File Into an Array in PHP
Use PHP to parse tab-delimited files to arrays. (Hemera Technologies/Photos.com/Getty Images)

PHP is an open source programming language that can be used to create dynamic web pages. PHP code placed in HTML executes on the server and PHP has a variety of built-in types, such as strings, objects and arrays. PHP offers a variety of functions for accomplishing many common tasks, such as opening and reading files, working with strings and writing values into arrays. PHP also has a variety of variable-handling functions that provide shortcuts for printing variables, retrieving variable data types and working with cookies.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Text editor
  • Web server with PHP 4 or 5 installed and configured
  • Web browser

Show MoreHide

Instructions

  1. 1

    Open a text editor and type some tab-delimited values in a new file. Enter the values by inserting a "tab" character in between each value. Save the file on the web server with the name "tabbedTextFile.txt", noting the file's location. The "(tab)" text is shown to illustrate the tabs separating each value. Save and close tabbedTextFile.txt.

    value 1(tab)value 2(tab)value 3(tab)value 4(tab)value 5

  2. 2

    Return to the text editor and type a "<?php" and a "?>" PHP delimiter. These delimiters tell the server to interpret any code placed between them as PHP code. Open tabbedTextFile.txt for reading using the PHP "fopen()" function and write the returned value to a variable named "$openFile". Save the file as "parseToArray.php".

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    ?>

  3. 3

    Continue editing parseToArray.php. Use the PHP "fread()" function to read the contents of tabbedTextFile.txt to a variable named "$fileContents". Use the PHP "filesize()" function to indicate that the entire file should be read.

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    $file Contents = fread($open File, filesize("tabbedTextFile.txt"));

    ?>

  4. 4

    Continue editing parseToArray.php and enter the PHP "fclose()" function. This function closes tabbedTextFile.txt.

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    $file Contents = fread($open File, filesize("tabbedTextFile.txt"));

    fclose($openFile);

    ?>

  5. 5

    Continue editing parseToArray.php. Identify the delimiter (tab) that should be used when parsing tabbedTextFile.txt. Store the delimiter to a variable named "$delimiter".

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    $file Contents = fread($open File, filesize("tabbedTextFile.txt"));

    fclose($openFile);

    $delimiter = " ";

    ?>

  6. 6

    Continue editing parseToArray.php. Use the PHP "explode()" function to extract the values in tabbedTextFile.txt, specifying that delimiter stored in $file Contents (tab). Write the exploded values to a variable named "$myArray". The "explode()" function returns an array of values.

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    $file Contents = fread($open File, filesize("tabbedTextFile.txt"));

    fclose($openFile);

    $delimiter = " ";

    $my Array = explode($delimiter, $file Contents);

    ?>

  7. 7

    Continue editing parseToArray.php. Print the values of $my Array array using the "print_r" command. The "print_r" command prints readable information about a variable. Save and close parseToArray.php.

    <?php

    $open File = fopen("tabbedTextFile.txt", "r");

    $file Contents = fread($open File, filesize("tabbedTextFile.txt"));

    fclose($openFile);

    $delimiter = " ";

    $my Array = explode($delimiter, $file Contents);

    print_r($myArray);

    ?>

  8. 8

    Open a web browser and access parseToArray.php on the web server. Verify that the array values are printed on the web page.

Tips and warnings

  • The "print_r" command is not the only way to display an array's values. Looping through the array can retrieve these values and the array can be manipulated with PHP's built-in array functions.
  • PHP requires proper configuration on the web server. See the Resources section for information on PHP installation and configuration.
  • The behaviour of variable-handling functions is affected by the settings in php.ini.

Don't Miss

Resources

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.