Jump to content

Redirect output to a file?


anonp

Recommended Posts

Hello,

In a .php file, text that is outside of <?php and ?> is output to either the client browser or stdout. Likewise, echo is output to the client browser or stdout. Is it possible to redirect this output to a file?

I am using PHP to generate Java based on some input from the user. I want to save the output of my php into a .java file.

For example:

class HelloWorldApp {
public static void main(String[] args) {
//Display "Hello World!"
System.out.println("<?php echo $some_user_input ?>");
<?php
for ($i = 1; $i <= $some_other_input; $i++) {
echo $i;
}
?>
}
}

Please help. Thanks
Link to comment
Share on other sites

You could use PHPs file functions...
[code]<?php
$java = "class HelloWorld {\r\n
\tpublic static void main (String[] arguments) {\r\n
\t\tSystem.out.println(\"Hello World\");\r\n
\t}\r\n
}";

$h = fopen ("hello_world.java","wb");
fwrite ($h,$java);
fclose ($h);
?>[/code]
Don't forget the semi-colon on the end of the file functions, they're removed in my example due to a bug in the forum.
Link to comment
Share on other sites

Hi, SA I found out you can put space before the opening parenthesis on functions:
[code]fopen ();[/code]
and it'll allow you to post your code, but the good thing is PHP still parses the code with the space before the perenthesis. I modified your post. I hope you didn't mind, :)
Link to comment
Share on other sites


Hi, Thank you for the responses. Yes, I can use the file functions, but it is not as clean as redirecting the output. Notice all the "\" added to the text. In addition, I cannot put complex statements such as the for-loop into a string. Basically, Yes, file functions will work, but I am hoping for a more elegant solution.

Any other suggestion? Thanks
Link to comment
Share on other sites

Thos \r or \t chars are shortcuts for manually entering in a return or entering in four spaces etc in PHP. However you will still need to use the \ character to escape quotes incase your Java code has double quotes in etc. So you can it like this:
[code]<?php
$java = "class HelloWorld {
    public static void main (String[] arguments) {
       System.out.println(\"Hello World\");
     }
}";

$h = fopen ("hello_world.java","wb");
fwrite ($h, $java);
fclose ($h);
?>[/code]
However when you file is written those backslashes or whitespace characters (\r\n) etc arn't going be seen in the file it is being wrtitten to. They are only there for PHP, especially the escaping of the double quotes. Becuase when you start a string with a double quote, and you have a double quote half through your string PHP will think you have finished defining the string. When the you escape the double quote it'lll continue on until finds another unescaped double quote, which should be the last one you have finished defining a string.

Hope that helps.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.