Is there any way to get a simple php code that just displays the text from a txt document, that's on a url?
This is what I've got so far. I'm sure I'm missing something~
<?
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
fopen ($text)
?>
Guessing you can't open it, since it's not on the drive. Any workaround that, or fix you guys could help me with?
Thanks (:
Too easy! :)
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
Explanation:
file_get_contents() will return the contents of the remote file and assign it to the $text variable. Then you'll just have to ouput these contents using the echo statement
Alternative: use the readfile() function. It will output the contents of the file directly:
readfile('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
You don't have to open it. It's already appended as a string to the variable $text.
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
//Outputs "“Why Don't You Get A Job?” ― The Offspring"
I'd like it if ob_start() didn't let echo's output to their normal destination and just log their contents instead. But it doesn't seem to be doing that. Any ideas? Here's my code:
<?php
ob_start();
echo 'test';
$out = ob_get_contents();
var_dump($out);
test is still echo'd. It's var_dump'd, as well, but I don't want it to be echo'd.
Any ideas?
Thanks!
The output buffer is automatically flushed at the end of the script, so it's expected behaviour.
You are looking for ob_get_clean(), which returns the current buffer before clearing it:
$out = ob_get_clean();
When I use CocoaRestClient to submit a GET on an HTML website it will return the source for the site. How can I return the same thing as a string (or something to convert into a string or parse) in php? I've tried using
echo $_GET[$url];
but it does not seem to return anything.
Note: the string returned will probably be rather large.
If you are asking about how to make an HTTP request from PHP and get the response as a string, for the simplest cases you can use file_get_contents:
$html = file_get_contents('http://www.google.com');
If you want to do something more configurable then you have to go with curl.
Do you want to prevent sending the source to the visitor's browser and get it as a string instead? You can use output buffer.
Put this line at the beginning of your code:
ob_start();
From now on everything that would normally be sent to the browser will be buffered instead. To end buffering and get contents use:
$out = ob_get_contents(); // this will only get the contents
$out = ob_get_clean(); // this will also empty the buffer
To stop buffering:
ob_end_clean(); // will just stop buffering
ob_end_flush(); // will also echo() current buffer
Output buffer docs here.
I'm using a PHP library that echoes a result rather than returns it. Is there an easy way to capture the output from echo/print and store it in a variable? (Other text has already been output, and output buffering is not being used.)
You could use output buffering :
ob_start();
function test ($var) {
echo $var;
}
test("hello");
$content = ob_get_clean();
var_dump($content); // string(5) "hello"
But it's not a clean and fun syntax to use. It may be a good idea to find a better library...
The only way I know.
ob_start();
echo "Some String";
$var = ob_get_clean();
You should really rewrite the class if you can. I doubt it would be that hard to find the echo/print statements and replace them with $output .=. Using ob_xxx does take resources.
Its always good practise not to echo data until your application as fully completed, for example
<?php
echo 'Start';
session_start();
?>
now session_start along with another string of functions would not work as there's already been data outputted as the response, but by doing the following:
<?php
$output = 'Start';
session_start();
echo $output;
?>
This would work and its less error prone, but if its a must that you need to capture output then you would do:
ob_start();
//Whatever you want here
$data = ob_get_contents();
//Then we clean out that buffer with:
ob_end_clean();
I was wondering how to save PHP variables to a txt file and then
retrieve them again.
Example:
There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
This should do what you want, but without more context I can't tell for sure.
Writing $text to a file:
$text = "Anything";
$var_str = var_export($text, true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
Retrieving it again:
include 'filename.php';
echo $text;
Personally, I'd use file_put_contents and file_get_contents (these are wrappers for fopen, fputs, etc).
Also, if you are going to write any structured data, such as arrays, I suggest you serialize and unserialize the files contents.
$file = '/tmp/file';
$content = serialize($my_variable);
file_put_contents($file, $content);
$content = unserialize(file_get_contents($file));
(Sorry I can't comment just yet, otherwise I would)
To add to Christian's answer you might consider using json_encode and json_decode instead of serialize and unserialize to keep you safe. See a warning from the PHP man page:
Warning
Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
So your final solution might have the following:
$file = '/tmp/file';
$content = json_encode($my_variable);
file_put_contents($file, $content);
$content = json_decode(file_get_contents($file), TRUE);
for_example, you have anyFile.php, and there is written $any_variable='hi Frank';
to change that variable to hi Jack, use like the following code:
<?php
$content = file_get_contents('anyFile.php');
$new_content = preg_replace('/\$any_variable=\"(.*?)\";/', '$any_variable="hi Jack";', $content);
file_put_contents('anyFile.php', $new_content);
?>
Use a combination of of fopen, fwrite and fread. PHP.net has excellent documentation and examples of each of them.
http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fwrite.php
http://us2.php.net/manual/en/function.fread.php
Use serialize() on the variable, then save the string to a file. later you will be able to read the serialed var from the file and rebuilt the original var (wether it was a string or an array or an object)
Okay, so I needed a solution to this, and I borrowed heavily from the answers to this question and made a library: https://github.com/rahuldottech/varDx (Licensed under the MIT license).
It uses serialize() and unserialize() and writes data to a file. It can read and write multiple objects/variables/whatever to and from the same file.
Usage:
<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create an object
$dx->def('file.dat'); //define data file
$val1 = "this is a string";
$dx->write('data1', $val1); //writes key to file
echo $dx->read('data1'); //returns key value from file
See the github page for more information. It has functions to read, write, check, modify and delete data.