0

I am trying to get PHP to search a text file for a string. I know the string exists in the text, PHP can display all the text, and yet strpos returns false. Here is my code:

<?php
    $pyscript = "testscript.py";
    //$path = "C:\\Users\\eneidhart\\Documents\\Python Scripts\\";
    $process_path = "C:\\Users\\eneidhart\\Documents\\ProcessList.txt";
    //$processcmd = "WMIC /OUTPUT: $process PROCESS get  Caption,Commandline,Processid";
    $process_file = fopen($process_path, "r") or die("Unable to open file!");
    $processes = fread($process_file);
    if (strpos($processes, $pyscript) !== FALSE) {
        echo "$pyscript found";
    } elseif (strpos($processes, $pyscript) === FALSE) {
        echo "$pyscript NOT found :(";
    } else {
        echo "UHHHHHHHH...";
    }
    echo "<br />";
    while (!feof($process_file)) {
        echo fgets($process_file)."<br />";
    }
    fclose($processfile);
    echo "End";
?>

The while loop will print out every line of the text file, including

python.exe                     python  testscript.py

but strpos still can't seem to find "testscript.py" anywhere in it.
The final goal of this script is not necessarily to read that text file, but to check whether or not a particular python script is currently running. (I'm working on Windows 7, by the way.) The text file was generated using the commented out $processcmd and I've tried having PHP return the output of that command like this:

$result = `$processcmd`;

but no value was returned. Something about the format of this output seems to be disagreeing with PHP, which would explain why strpos isn't working, but this is the only command I know of that will show me which python script is running, rather than just showing me that python.exe is running. Is there a way to get this text readable, or even just a different way of getting PHP to recognize that a python script is running?

Thanks in advance!

EDIT: I think I found the source of the problem. I created my own text file (test.txt) which only contained the string I was searching for, and used file_get_contents as was suggested, and that worked, though it did not work for the original text file. Turns out that the command listed under $processcmd creates a text file with Unicode encoding, not ANSI (which my test.txt was encoded in). Is it possible for that command to create a text file with a different encoding, or even simpler, tell PHP to use Unicode, not ANSI?

  • 1
    At least according to [the documentation](http://us.php.net/manual/en/function.fread.php), `fread` requires a second argument (`length`). Maybe you want to use [`file_get_contents`](http://us.php.net/manual/en/function.file-get-contents.php) instead? – Sean Bright Apr 08 '15 at 12:19
  • Also there is a warning in php.net like On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' seems like you are on windows and your fopen useing only "r" try with "rb" – Santa's helper Apr 08 '15 at 12:23
  • Did you try `file_get_contents()` or `file()`? You might find [this](http://stackoverflow.com/a/24008078/4641017) useful. – Captain Hypertext Apr 08 '15 at 12:29

3 Answers3

0

You can use the functions preg_grep() and file():

$process_path = "C:\\Users\\eneidhart\\Documents\\ProcessList.txt";

$results = preg_grep('/\btestscript.py\b/', file($process_path));    
if(count($results)) {
    echo "string was found";
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

You should follow the advice given in the first comment and use either:

 file_get_contents($process_path);

or

fread($process_file, filesize($process_path));

If that fix is not enough and there is actually a problem on strpos (which shouldn't be the case), you can use:

preg_match("/.*testscript\.py.*/", $processes)

NB: Really try to use strpos and not preg_match as it's not advised by the documentation.

Answers_Seeker
  • 468
  • 4
  • 11
0

Well, I found the answer. Thanks to those of you who suggested using file_get_contents(), as I would not have gotten here without that advice. Turns out that WMIC outputs Unicode, and PHP did not like reading that. The solution was another command which converts Unicode to ANSI:

cmd.exe /a /c TYPE unicode_file.txt > ansi_file.txt

I hope this helps, for those of you out there trying to check if a particular python script is working, or if you're just trying to work with WMIC.