Forum Moderators: coopster

Message Too Old, No Replies

PHP for OS tasking...

         

MonkeeSage

11:10 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am in the midst of learning Perl, and XSL, and I figured I might as well add another item to the r'epertoire. I'm thinking about PHP, but I have a question--I know I've heard people say they use PHP for OS tasking, not just for use in HTML, but I'm not really sure how. Perhaps a practical example will help me out if the PHP experts would oblige me...

Let's say I want to run a script from the Windows command prompt that will read the contents of "test.txt" and evaluate each line, looking for "this is a test", and then print out the line number and line value if found...

The Perl would be something to the effect...

#!/usr/bin/perl 
use warnings;
use strict;

open(FHANDLE, "<test.txt");
my @lines = <FHANDLE>;
close(FHANDLE);

my $count = 0;
foreach my $line (@lines) {
$count++;
if ($line =~ /this is a test/) {
print "\n$count: $line\n";
}
}

How would I do this in PHP? Thanks in advance!

Jordan

jatar_k

11:16 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



one problem

what does this mean verbally?

=~

MonkeeSage

11:22 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, sorry about that, it means 'matches pattern'

Jordan

jatar_k

11:26 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<?
$fp = fopen("test.txt","r+");
while (!feof($fp)) {
$linearr[] = fgets($fp);
}

$count = 0;
foreach ($linearr as $line) {
$count++;
if (strcmp($line,"this is a test")) { //**
print "\n$count: $line\n";
}
}
?>

** could also use
if (preg_match('/this is a test/',$line) {
but then again I could have messed up that syntax

I had the general notion but have never asked that so thought it was a good time ;)

MonkeeSage

11:43 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks alot! I'm downloading the PHP 4.3.3 package right now, got the documentation open and am off to play. :)

Ps. I assume I run the script with the binary...

c:\>php test.php

...that right?

Jordan

jatar_k

11:48 pm on Sep 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess, I've never run it on win

my precious php under the control of windows
*shudder*
;)

MonkeeSage

12:09 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lol!

Actually that is part of the reason why I'm learning Perl (and I guess PHP now :) ), because I want to make a couple CLI apps that will run somewhat transparently on this WinXP box, my other RedHat box and my brother's GenToo box. Aside from Perl / PHP the only other options were actual binaries in C# using Ximian mono, or Javabeans/JFC/SWING...C# looked good, but the windows port of mono is very(x5) buggy, and Java is...well...Java. ;)

PHP seems to be very powerful from everything I've been reading, I think I'll be able to accomplish exactly what I'm trying to. :)

Thanks again for the example.

Jordan

MonkeeSage

12:28 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I used the preg_match('/this is a test/', $line) and it worked perfectly. Output is 1:1 with the Perl script, with one difference...

This always appears on the command line above the script output...

"Content-type: text/html
X-Powered-By: PHP/4.3.3"

This would be a great time-saver in the browser...but I don't really want it on the command line. ;) I tried adding a shebang I found on PHPFreaks: #!/usr/bin/php -q (I assumed the -q was for quiet), but it still prints out that header.

Any idea how to turn that off?

Jordan

MonkeeSage

12:44 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NM, there is a cli subfolder under PHP/ with the command line binary that doesn't put out the headers, sweet. :)

Jordan

daisho

12:58 am on Sep 25, 2003 (gmt 0)

10+ Year Member



the -q "quiet" is what you want. Pound Bang is for a magic cookie on linux.

in windows do:

php -q test.php

on linux you can do the same *OR* as the first line of the script (before <?) put:

#!/path/to/php -q

then to "chmod a+x test.php"

now you can just do ./test.php

when bash (or every shell that I know of) is asked to execute a text file it will look for #! and if it sees it the rest of the file will be fed to whatever is after the!.

That's why you have:

#!/bin/bash
or
#!/bin/perl
and now
#!/usr/local/bin/php
:)

daisho.

MonkeeSage

1:15 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks daisho, works great. :)

Jordan