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
Running PHP Code on Every Line of an Input File in PHP
PHP

Running PHP Code on Every Line of an Input File in PHP

InfinityCoder December 27, 2016

You want to read an entire file and execute PHP code on every line. For example, you want to create a command-line version of grep that uses PHP’s Perl-compatible regular expression engine.

Use the -R command-line flag to process standard input:

1
2
3
4
5
6
% php -R 'if (preg_match("/$argv[1]/", $argn)) print "$argn\n";'
 
      php
      < /usr/share/dict/words
 
ephphatha

To execute a block of code before or after processing the lines, use the -B and -E options, respectively:

1
2
3
4
5
6
7
8
% php -B '$count = 0;'
 
      -R 'if (preg_match("/$argv[1]/", $argn)) $count++;'
      -E 'print "$count\n";'
      php
      < /usr/share/dict/words
 
l

Sometimes you want to quickly process a file using PHP via the command line, either as a standalone project or within a sequence of piped commands.

This lets you whip up a quick-and-dirty script to transform data. PHP makes that easy using three command-line flags and two special variables: -R, -B, -E, $argn, and $argi.
The -R flag specifies the PHP code you want to execute for every line in the file. Within that block of code, you can access the line’s text in the $argn variable.
As a basic example, here’s a PHP script that takes HTML input, strips the tags, and prints out the result:

1
php -R 'print strip_tags($argn) . "\n"; ' < index.html

Because PHP automatically strips the newline from the end of the input, this code not only displays the results of strip_tags($argn), but also echos a newline.
It operates on the file index.html, which is passed in as standard input. There is no mechanism for specifying the file that you want processed.
This slightly more complicated example, which is a simple version of grep, shows how to accept input arguments via the $argv array:

1
2
3
4
5
6
% php -R 'if (preg_match("/$argv[1]/", $argn)) print "$argn\n";'
 
      php
      < /usr/share/dict/words
 
ephphatha

The first value passed to preg_match() is /$argv[1]/, which is the first argument passed to the script. In this example, it’s php, so this code is searching for all the words in the /usr/share/dict/words file containing php.
For what it’s worth, ephphatha is an Aramaic word meaning be opened.
Beyond the individual lines, you sometimes need to execute initialization or clean-up code. Specify this using the -B and -E flags.
Building on the grep example, this code counts the total number of matching lines:

1
2
3
4
5
6
7
8
% php -B '$count = 0;'
 
      -R 'if (preg_match("/$argv[1]/", $argn)) $count++;'
      -E 'print "$count\n";'
      php
      < /usr/share/dict/words
 
1

Inside the -B block, you initialize the $count to 0. It’s then incremented in the -R block whenever there’s a match.

Finally, the total number is printed out in the -E block.
To find out the percentage of matching lines, in addition to the total, use $argi:

1
2
3
4
5
6
7
8
% php -B '$count = 0;'
 
      -R 'if (preg_match("/$argv[1]/", $argn)) $count++;'
      -E 'print "$count/$argi\n";'
      php
      < /usr/share/dict/words
 
1/234937

The $argi variable contains the current line number of the file, so inside the -E block, it’s set to the total number of lines.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Parsing Basic XML Documents in PHP
You want to parse a basic XML document that follows …

Parsing Basic XML Documents in PHP

Using Named Parameters in PHP
You want to specify your arguments to a function by …

Using Named Parameters 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