anonp Posted June 23, 2006 Share Posted June 23, 2006 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 ?>");<?phpfor ($i = 1; $i <= $some_other_input; $i++) { echo $i;}?> }}Please help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/12741-redirect-output-to-a-file/ Share on other sites More sharing options...
zq29 Posted June 23, 2006 Share Posted June 23, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12741-redirect-output-to-a-file/#findComment-48861 Share on other sites More sharing options...
wildteen88 Posted June 23, 2006 Share Posted June 23, 2006 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, :) Quote Link to comment https://forums.phpfreaks.com/topic/12741-redirect-output-to-a-file/#findComment-48886 Share on other sites More sharing options...
anonp Posted June 24, 2006 Author Share Posted June 24, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12741-redirect-output-to-a-file/#findComment-48962 Share on other sites More sharing options...
wildteen88 Posted June 24, 2006 Share Posted June 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12741-redirect-output-to-a-file/#findComment-49043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.