PHP exec capture text - php

I am having a problem with capturing text when I make an exec call to a perl script that just prints a lot of text. What happens when I run the following code is I get a 1 word result: "Array". I need to be able to capture the results so that I can change them around just a little bit. Here is the code:
<?php
$lastline = exec("perl parseOutput.pl",$retVal);
echo $retVal;
?>
How do I work around this?

You have an array of the lines of text that was outputted.
Do something like this:
echo implode( "\n", $retVal);
Or
echo implode( "<br />\n", $retVal);
And you'll see all of the output generated by the perl script.

just use shell_exec()
$fullResult = shell_exec("perl parseOutput.pl");
echo $fullResult;

Related

How to get a certain text from this function?

I'd like to know how to get the text Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx from $cmd = system('ipconfig /all').
When I write $ cmd = system('ipconfig /all') there is atext displayed in my screen. Even without echoing the $cmd. How to remove the text but some portion of the text. I'd like to get 38 chars after the text Physical Address
note: more importantly to get a rid of the displayed text of system('ipconfig /all') but still contain it in a variable
You need to test a bit because i dont know exactly your IPCONFIG results (varies from OS to OS) but here is a very close to what you want:
<?php
$command = "ipconfig /all";
$output = shell_exec($command);
$output = explode(PHP_EOL, $output);
$output = $output[0];
$result = explode("Physical Address",$output);
echo substr($result[1],20,17);
?>
Keep in mind you may need to change echo substr($result[1],20,15); to fit your needs, as it varies depending how many network cards you have etc.
In addition to Mohammad's answer you could use output buffering.
something like:
ob_start();
system('ipconfig /all');
$output = ob_get_clean();
print_r( $output);
should capture everything coming back from the command. You can then manipulate it however you like as a string.
$val = shell_exec('ipconfig /all');
echo $val;
You can send second parameter to system function to get output of command:
system('ipconfig /all', $output); //$output is result of command
Also you can use shell_exec function to only return output and not echo by default as below:
$output = shell_exec('ipconfig /all');

PHP trim() not trimming space

I have run into a strange problem. I am trying to trim a string using php trim(), but it is not working.
Here is my code
echo $deal->{self::DEAL_BUSINESS};
echo '<br>';
echo trim($deal->{self::DEAL_BUSINESS});
echo '<br>';
And here is the output
Alkaram Studio
Alkaram Studio
If it is not clear from the output. There is a space in the beginning of both untrimmed and the trimmed string.
In the view source I got this.
Alkaram Studio
Try the following:
echo trim($deal->{self::DEAL_BUSINESS}, "\xC2\xA0\n");
Or
$text = preg_replace('~\x{00a0}~siu','',$deal->{self::DEAL_BUSINESS});
echo $text;

Executing Python Script with PHP Variables

I am writing a simple application that uses information from a form, passes it through $_POST to a PHP script that executes a python script and outputs the results. The problem I am having is that my python script is not actually running with the arguments being passed in.
process3.php file:
<?php
$start_word = $_POST['start'];
$end_word = $_POST['end'];
echo "Start word: ". $start_word . "<br />";
echo "End word: ". $end_word . "<br />";
echo "Results from wordgame.py...";
echo "</br>";
$output = passthru('python wordgame2.py $start_word $end_word');
echo $output;
?>
Output:
Start word: dog
End word: cat
Results from wordgame.py...
Number of arguments: 1 arguments. Argument List: ['wordgame2.py']
At the top of my wordgame2.py, I have the following (for debugging purposes):
#!/usr/bin/env python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Why isn't the number of arguments being passed = 3?
(Yes, my form does send the data correctly.)
Any help is greatly appreciated!
Edit: I might add that it does run when I explicitly tell it the start and end word... something like this:
$output = passthru('python wordgame2.py cat dog');
echo $output
Update -
Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,
$output = passthru("python wordgame2.py $start_word $end_word");
would work, but the following won't -
$output = passthru('python wordgame2.py $start_word $end_word');
Original answer -
I think the mistake lies in
$output = passthru("python wordgame2.py $start_word $end_word");
Try this
$output = passthru("python wordgame2.py ".$start_word." ".$end_word);
Thank you for your contributions. I have figured out my problem with this simple fix:
$command = 'python wordgame2.py ' . $start_word . ' ' . $end_word;
$output = passthru($command);
In order for passthru to properly handle the php variables, it needs to be concatenated into the string before executing.
well if I understood you want pass a big amount of text like content of something, so the right way is;
$output = passthru("python wordgame2.py ".json_encode($end_word)." ".json_encode($start_word));

Explode on new line in PHP only reads first line

So my input is a number of lines :
AAAAA
CCSDCSDC
jhbhvhv
I use explode in PHP to have an array, each array entry is a line:
$lines=explode("\n", $text);
so when I do this
echo $lines[0];
echo $lines[1];
echo $lines[2];
I get only the first line :
AAAAA
What goes wrong ?
<?php
$str = "AAAAA\nCCSDCSDC\njhbhvhv";
$lines=explode("\n", $str);
echo $lines[0]."<br/>";
echo $lines[1]."<br/>";
echo $lines[2]."<br/>";
?>
I'm not sure, but does it make a diffrence where the php script is running on?
I mean \n for UNIX systems and \r\n for Windows. Maybe you could try this:
$lines=explode("\r\n", $text);

How can I make a timer animation in the command line?

This is a little silly, but something I've wanted to do before and could never figure out. I have a PHP script that runs from the command line. I'd like a little timer animation to let the user know the script is still running. Here's what I have:
while (1 == 1) {
echo '—';
usleep(100000);
echo '\';
usleep(100000);
echo '|';
usleep(100000);
echo '/';
}
But how do I get each echo to replace the character before it?
You need to print a backspace before each character, for example:-
echo '-';
usleep(100000);
echo "\b/";
Note that you have to use double quotes here or the escape sequence won't work.
I'm sure you can work the rest out :)
If "\b" doesn't work try:-
echo chr(8) . '/';
Try the php ncurses extension:
http://php.net/manual/en/ref.ncurses.php
You can use
echo "yourCharacter1\r";
usleep(100000);
echo "yourCharacter1\r";
or
echo "\ryourCharacter1\r";
usleep(100000);
echo "\ryourCharacter2\r";
\r sends the cursor back to position 0 on the same line.
Also you may try (found online)
system("clear"); // before you echo new characters
or
passthru('clear'); // before you echo new characters

Categories