0

I managed to create a new line for each only with :

$content1= hot("britney") ? "britney found" : "";
$content2= hot("gaga") ? "gaga found" : "";
$content3= hot("carol") ? "carol found" : ""; 

$filename = 'result.txt';
$handle = fopen($filename, 'a');
fwrite($handle, "$Content1\r\n");
fwrite($handle, "$Content2\r\n");
fwrite($handle, "$Content3\r\n");
fwrite($handle, "$Content4\r\n");
fclose($handle);

But i have many lines and it makes a lot of modifications... How could i automatize the process ? maybe something like foreach < i really don't know how to implement this one here'

I have the following codes:

require("class.XMLHttpRequest.php");
function hot($news){
    $url="https://localhost/search.aspx?search=".$news.""; 
 $ajax=new XMLHttpRequest();
 $ajax->setRequestHeader("Cookie","Cookie: host");
 $ajax->open("GET",$url,true);
 $ajax->send(null);
 if($ajax->status==200){
  $rHeader=$ajax->getResponseHeader("Set-Cookie");
  if(substr_count($rHeader, "Present!")>0) { return true; }
 }else{ return false; }
} 

echo ( hot("britney") )?"britney found":"<br>";
echo ( hot("gaga") )?"gaga found":"<br>";
echo ( hot("carol") )?"carol found":"<br>"; 

AND

<?php
$filename = 'test.txt';
$Content = "Add this to the file\r\n";

echo "open";
$handle = fopen($filename, 'x+');
echo " write";
fwrite($handle, $Content);
echo " close";
fclose($handle);
?>

and i have many echo ( hot("britney") )?"britney found":"<br>"; in my script and because of that i want to send them to a file

how can i set a string for echo ( hot("britney") )?"britney found":"<br>";to be able to use it in my code that sends the info to file

i also don't want the page to print anything to the screen

3
  • 1
    your parentheses are incorrectly placed. remove them from the echo call entirely Commented Sep 30, 2010 at 1:27
  • it works like this for me i get the response on first page Commented Sep 30, 2010 at 1:28
  • i just want to get the output in a file and not screen Commented Sep 30, 2010 at 1:29

5 Answers 5

1

Just store stuff in a variable then write variable.

// put stuff in $content instead of printing
$content = '';
$content .= hot("britney") ? "britney found" : "<br>";
$content .= hot("gaga") ? "gaga found" : "<br>";
$content .= hot("carol") ? "carol found" : "<br>"; 

// write to file
$handle = fopen($filename, 'x+');
fwrite($handle, $content);
fclose($handle);
Sign up to request clarification or add additional context in comments.

Comments

1

$Content = hot("britney") ? "britney found" : "<br>";

Comments

1

As others have already stated, put the contents of your echos into a variable then write that into a file. There are two ways to do this. You can use file handlers:

<?php
// "w" will create the file if it does not exist and overwrite if it does
// "a" will create the file if it does not exist and append to the end if it does
$file = fopen('/path/to/file', 'w');
fwrite($file, $content);
fclose($file);

A slightly simpler way is to use file_put_contents():

<?php
file_put_contents('/path/to/file', $contents);

And if you want to append to the file:

<?php
file_put_contents('/path/to/file', $contents, FILE_APPEND);

As for the parenthesis you have around the conditional for your echos, I prefer something more like the following:

<?php
$contents = '';
$contents .= (hot('britney') ? 'britney found' : '<br />');

If you want to be able to easily read the file outside of a web browser, you should use a new line instead of a <br /> to separate your output. For example:

<?php
$contents = '';
$contents .= (hot('britney') ? 'britney found'."\n" : "\n");

Comments

0

Replace all of the echo statements with a variable, then write that variable to a file.

$Content.= ( hot("britney") )?"britney found":"<br>";

etc...

2 Comments

it seems i cant make new line with anything $Content.= ( hot("britney") )?"britney found":"<br>"; br or \n or anything
@adam HTML requires <br> for EOL and ASCII requires \n.
0

First off, your parentheses are in the wrong place. Should be like this:

echo ( hot("britney") ? "britney found" : "
" );

If you want to capture your echo and other output, use the ob_start and ob_flush methods.

But if you're making HTTP requests to that file it won't echo on the screen.

If that's what you meant, then that's your answer.

5 Comments

it;s working now, i'm trying to add a new line after each discovery
i'm preety new to php i dont know to use ob_start and ob_flush yet
i'm preety new to php i dont know to use ob_start and ob_flush yet
Really easy to do: ob_start(); echo 'stuff'; $_output = ob_get_flush(); After that, whatever would have gone to the screen will be in the $_output variable.
@adam Thanks bro. Come see me sometime. Got them hoes you like!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.