How do you run Python/Perl scripts and return the output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eyevee99
Forum Newbie
Posts: 3
Joined: Sun May 01, 2005 2:36 am

How do you run Python/Perl scripts and return the output

Post by eyevee99 »

Hi,

I'm writing a simple CMS for a friend and have a query regarding running perl or python scripts from within php and returning the output for use in a HTML page.

eg, the user inputs some text and submits it to the database. A script parses that text before it is saved. Then when it is a retrieved another script parses it before it is displayed.

I'm implementing Markdown via Markdown.pl and html2text via html2text.py.

Both of these scripts run and work ine on the command line. However I am having significant trouble getting them to work from within php.

I'm currently using "system()" to run them, but I can't work out how to use the output.

I want to write a function that a I pass a string to and it returns the parsed output.

eg,

Code: Select all

function html2text($string)
{
  // parse text and return output
  ob_start();
  system("echo ".escapeshellarg($string)."|blah/html2text.py");
  $buffer             = ob_get_contents();
  ob_end_clean();

  return $buffer;
}

function text2html($string)
{
  ob_start();
  system("blah/Markdown.pl ".escapeshellarg($string));
  $buffer             = ob_get_contents();
  ob_end_clean();

  return $buffer;
}
This however always returns nothing. The reteurn code form system is "0" which I believe means executed without errors. Am I approaching this correctly?
Last edited by eyevee99 on Sun May 01, 2005 3:00 am, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

look at this example

Code: Select all

<?php
echo '<pre>';

// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls', $retval);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
you also never set $buffer
eyevee99
Forum Newbie
Posts: 3
Joined: Sun May 01, 2005 2:36 am

Post by eyevee99 »

Yeah I left a line out of my initial post, and between our edits it got lost again while I was trying to add it.

:)

Code: Select all

$buffer             = ob_get_contents();
eyevee99
Forum Newbie
Posts: 3
Joined: Sun May 01, 2005 2:36 am

Post by eyevee99 »

It was just a bracket in the wrong place. I've been coding for too long today... Thanks for your help.
Post Reply