<?php
$start_date = "20111101";
$todays_date = date('Ymd');
$html = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$start_date}.html?mod=mdc_pastcalendar");
preg_match_all(
'#<td style="text-align:left;padding-top:18px;" valign="bottom" class="colhead">(.*?)</td>#',
$html,
$EXCHANGE,
PREG_PATTERN_ORDER);
preg_match_all(
'#<td class="num">(\d.\d\d)</td>#',
$html,
$TRIN,
PREG_PATTERN_ORDER);
echo "{$EXCHANGE[0][0]}, \r";
echo "TRIN : {$TRIN[0][0]}, \r";
echo "{$EXCHANGE[0][1]}, \r";
echo "TRIN : {$TRIN[0][1]}, \r";
echo "{$EXCHANGE[0][2]}, \r";
echo "TRIN : {$TRIN[0][2]}, \r";
echo "{$EXCHANGE[0][3]}, \r";
echo "TRIN : {$TRIN[0][3]}, \r";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
?>
Why is the output in WSJData.csv different from the echo output? Why do I get the text I removed with the preg_match_all function in my CSV file?
If you want to write the result of some echo calls to a file, use:
ob_start(create_function('$a','file_put_contents(FILENAME,$a); return $a;'));
echo ...
echo ...
...
echo ...
ob_end_flush();
This will ensure that what is printed is what goes in the file.
Sorry, it seems to me that you have some other error in your script. I reduced it to "the simplest program that works" to reproduce the error. Here is what I got:
<?php
$TRIN = array( array("E0","E1","E2","E3") );
echo "TRIN : {$TRIN[0][0]} / ";
echo "TRIN : {$TRIN[0][1]} / ";
echo "TRIN : {$TRIN[0][2]} / ";
echo "TRIN : {$TRIN[0][3]} \n";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
echo file_get_contents($WSJData);
And the output is:
$ php test.php
TRIN : E0 / TRIN : E1 / TRIN : E2 / TRIN : E3
E0, E1, E2, E3
The first line of output is from the echoes. The second is from "file_get_contents".
The substitution works fine. Indeed, it occurs when assigning the variables, not when echoing or saving to the file.
BTW, I would recommend using \n instead of \r. The second one will not advance a line, but overwrite the current one (just in the console as it would in the printer), and I suspect that that's not what you wanted.
Related
I have PHP set up for a textarea that prints text from a directory to separate divs.
<?php
$content = implode("<br /><hr class='separator'>",array_map(function ($v) {
return file_get_contents($v);
}, glob(__DIR__ . "/posts/*.txt")));
echo '<div>';
// echo htmlentities($content);
echo '</div>';
?>
When I use htmlentities (commented out above), it disables my line break (and also my hr separator code).
I tried using \n but it also doesn't work.
How do I keep my <br /><hr class='separator'> code but still use htmlentities?
Call htmlentities() before inserting the HTML separators.
$content = implode("<br /><hr class='separator'>",array_map(function ($v) {
return htmlentities(file_get_contents($v));
}, glob(__DIR__ . "/posts/*.txt")));
echo "<div>$content</div>";
I'm try to run php tag inside a string like so:
$str = '<h1><?php echo Hello World ?></h1>';
echo $str;
But it returns
<h1><!-- ?php echo Hello World ? --></h1>
I want an output similar to this code:
ob_start();
require ("../view/file_name.inc");
$html = ob_get_contents();
ob_end_clean();
Without Using:
require
or
include
Using:
file_get_contents('../view/file_name.inc');
without templating as well
let say :
file_name.inc has this:
<h1><?php echo $title ?></h1>
<?php foreach($data as $d){ ?>
<p><?php echo $d->desc ?><p>
<?php } ?>
The output something like:
<h1>Hello</h1>
<p>desc1</p>
<p>desc2</p>
Use htmlentities function of PHP to achieve your goal.
$str = '<h1><?php echo Hello World ?></h1>';
echo htmlentities($str);
It will output
<h1><?php echo Hello World ?></h1>
EDIT
Use eval to execute php script in string. try below code
$str = '<h1><?php echo "Hello World" ?></h1>';
eval('?>'.$str.'<?php;');
echo $str;
this should be help you
$str = '<h1>Hello World</h1>';
I have this code for example:
ob_start();
echo "hello there";
$output = ob_get_contents();
return $output;
When I run it, I get back:
hello there
But how can I get back
echo "hello there";
Is there a way to do this easily?
To output arbitrary text as-is you can close the PHP script and then reopen it. Anything between the closing and opening tags is output as-is
ob_start();
?>echo "hello there";
<?php
$output = ob_get_contents();
return $output;
ob_get_contents will return the echoed output, so you can't use it to show actual code.
To simply print out code, I would try this:
$code = file_get_contents('your_code.php');
echo "<pre>{$code}</pre>";
Also you can write you code separatle as text and echo it or eval (if you need execution).
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
result:
This is a $string with my $name in it.
This is a cup with my coffee in it.
By representing it as a string:
ob_start();
$str = <<<STR
echo "hello there";
STR;
echo $str;
$output = ob_get_contents();
return $output;
For example if I had the script:
<?php
$page = "My Page";
echo "<title>" . $page . "</title>";
require_once('header.php');
require_once('content.php');
require_once('footer.php');
?>
Is there something I can add to the bottom of that page to show the entire pre-compiled php?
I want to literally echo the php code, and not compile it.
So in my browser I would see the following in code form...
// stuff from main php
$page = "My Page";
echo "<title>" . $page . "</title>";
// stuff from require_once('header.php');
$hello = "Welcome to my site!";
$name = "Bob";
echo "<div>" . $hello . " " . $name . "</div>";
// stuff from require_once('content.php');
echo "<div>Some kool content!!!!!</div>";
// stuff from require_once('footer.php');
$footerbox = "<div>Footer</div>";
echo $footerbox;
Is this possible?
There's no way to do it native to PHP, but you could try to hack it if you just wanted something extremely simplistic and non-robust:
<?php
$php = file_get_contents($_GET['file']);
$php = preg_replace_callback('#^\s*(?:require|include)(?:_once)?\((["\'])(?P<file>[^\\1]+)\\1\);\s*$#m', function($matches) {
$contents = file_get_contents($matches['file']);
return preg_replace('#<\?php(.+?)(?:\?>)?#s', '\\1', $contents);
}, $php);
echo '<pre>', htmlentities($php), '</pre>';
Notes:
Warning: Allowing arbitrary file parsing like I've done with the fist line is a security hole. Do your own authentication, path restricting, etc.
This is not recursive (though it wouldn't take much more work to make it so), so it won't handle included files within other included files and so on.
The regex matching is not robust, and very simplistic.
The included files are assumed to be statically named, within strings. Things like include($foo); or include(__DIR__ . '/foo.php'); will not work.
Disclaimer: Essentially, to do this right, you need to actually parse the PHP code. I only offer the above because it was an interesting problem and I was bored.
echo '$page = "My Page";';
echo 'echo "<title>" . $page . "</title>";';
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
For clarity I'd put the title generation in it's own file, then just use a series of echo file_get_contents()...
echo file_get_contents('title.php');
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
I have this code, which uses ob_start php function. Which basically puts the echoed data into an html file. It works before. I do not know what version of php I was using then. But my current version is 5.3.0. I cannot explain why it wouldn't work. Because the script below is working and it just puts the output of that script into the html file:
<?php
ob_start();
?>
<h2>Customer Payment Summary</h2>
<img id="tablez" src="../img/system/icons/Oficina-PDF-icon.png"></img>
<?php
if($amtopay>=$curcred){
$custchange=$amtopay - $curcred;
$newcred = 0;
echo "Change: ". $custchange."<br/>";
query_database("DELETE FROM sales_transaction WHERE Cust_Name='$customer'", "onstor", $link);
}else{
query_database("UPDATE customer_credit SET CREDIT='$newcred' WHERE Cust_Name='$customer'", "onstor", $link);
echo "Remaining Balance: ". $newcred."<br/>";;
}
echo "Customer: ".$customer."<br/>";
echo "Amount paid: ". $amtopay. "<br/>";
echo "Date: ". $date." ". date('A');
close_connection($link);
?>
<?php
file_put_contents('../tmp/customerpay.html', ob_get_contents());
?>
Here's the output of the code above:
But when I checked the html file which I specified in the file_put_contents. It gives me this. And I don't really understand why:
My problem is how to get the correct output from the html file that is being produced.
You aren't closing your output buffer before you do a file_put_contents...
At the end of your script change it to the following:
//...
close_connection($link);
$contents = ob_get_contents();
ob_end_clean();
file_put_contents('../tmp/customerpay.html', $contents);
?>