InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Reading Records with a Pattern Separator in PHP
PHP

Reading Records with a Pattern Separator in PHP

InfinityCoder December 24, 2016

You want to read in records from a file, in which each record is separated by a pattern you can match with a regular expression.

Read the entire file into a string and then split on the regular expression:

1
2
$contents = file_get_contents('/path/to/your/file.txt');
$records = preg_split('/[0-9]+\) /', $contents);

This breaks apart a numbered list and places the individual list items into array elements. So if you have a list like this:

1
2
3
1) Gödel
2) Escher
3) Bach

you end up with a four-element array, with an empty opening element. That’s because preg_split() assumes the delimiters are between items, but in this case, the numbers are before items:

1
2
3
4
5
6
7
8
9
10
11
12
13
array(4) {
  [0]=>
  string(0) ""
  [1]=>
  string(7) "Gödel
"
  [2]=>
  string(7) "Escher
"
  [3]=>
  string(5) "Bach
"
}

From one point of view, this can be a feature, not a bug, because the nth element holds the nth item. But, to compact the array, you can eliminate the first element:

1
2
$records = preg_split('/[0-9]+\) /', $contents);
array_shift($records);

Another modification you might want is to strip newlines from the elements and substitute the empty string instead:

1
2
$records = preg_split('/[0-9]+\) /', str_replace("\n",'',$contents));
array_shift($records);

PHP doesn’t allow you to change the input record separator to anything other than a newline, so this technique is also useful for breaking apart records divided by strings.
However, if you find yourself splitting on a string instead of a regular expression, substitute explode() for preg_split() for a more efficient operation.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Exposing Clean Resource Paths in PHP
You want your URLs to look clean and not include …

Exposing Clean Resource Paths in PHP

Generating a High-Precision Time in PHP
You need to measure time with finer than one-second resolution—for …

Generating a High-Precision Time in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap