I need to be able to get all of this information (as text) into the variable $all so that I can use it later in my script. But when I echo $all later on it doesn't work. And don't anyone say anything about the use of font tags, I'm as depressed about it as you are.
$all = <<< STOPTHISCRAZYTHING
echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
{
echo "<img src=\"" . $match . "\">";
}
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
{
if(!in_array($diff, $reject))
{
echo "<img src=\"" . $diff . "\">";
}
}
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
echo "<img src=\"" . $unabletoget . "\">";
}
echo "</textarea>";
STOPTHISCRAZYTHING;
You are looking for output buffering:
ob_start(); // Start capturing the script's output
echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
{
echo "<img src=\"" . $match . "\">";
}
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
{
if(!in_array($diff, $reject))
{
echo "<img src=\"" . $diff . "\">";
}
}
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
echo "<img src=\"" . $unabletoget . "\">";
}
echo "</textarea>";
$all = ob_get_flush(); // Stop capturing output, and store the output
// that was captured up until now into the variable $all
You can't embed looping constructs in heredoc syntax. You need to handle your foreach loops outside of the:
$all = STOPTHISCRAZYTHING
...
STOPTHISCRAZYTHING;
EDIT:
Ditto on the 'echo' statements. You can use variables in your heredoc assignment, but you can think of it as the whole block being the right side of a string assignment. Not a block in which you execute PHP commands.
EDIT 2:
Here's a valid example, using your example (part of it)
$all = <<< STOPTHISCRAZYTHING
<br><br><textarea rows="30" cols ="100">
<div align="center"><font size="7">I Have</font></div>
STOPTHISCRAZYTHING;
...note how you just enter the text you need, you don't have to echo, nor do you have to escape quotes.
docs: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Cheers
I'm not sure what you mean with It's all messed up, though it's printed.
but it seems to me it gets printed just fine but the browser is showing you the rendered version: Just check the source code.
What you need to do if you want to see it as real text is:
<pre>
tags to preserve the line-breaks (or use nl2br()
on the string before echoing it)htmlspecialchars()
on the variable before you echo it so that the <
etc. symbols get converted to html entities.Solution A :
<?php
$all = <<< STOPTHISCRAZYTHING
echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
{
echo "<img src=\"" . $match . "\">";
}
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
{
if(!in_array($diff, $reject))
{
echo "<img src=\"" . $diff . "\">";
}
}
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
echo "<img src=\"" . $unabletoget . "\">";
}
echo "</textarea>";
STOPTHISCRAZYTHING;
echo $all;
?>
Solution B (use htmlentities
):
<?php
$all = <<< STOPTHISCRAZYTHING
echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
{
echo "<img src=\"" . $match . "\">";
}
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
{
if(!in_array($diff, $reject))
{
echo "<img src=\"" . $diff . "\">";
}
}
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
echo "<img src=\"" . $unabletoget . "\">";
}
echo "</textarea>";
STOPTHISCRAZYTHING;
echo htmlentities($all);
?>
Solution C (wrap it in <pre>
...</pre>
tags):
<?php
$all = <<< STOPTHISCRAZYTHING
<pre>
echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
{
echo "<img src=\"" . $match . "\">";
}
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
{
if(!in_array($diff, $reject))
{
echo "<img src=\"" . $diff . "\">";
}
}
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
echo "<img src=\"" . $unabletoget . "\">";
}
echo "</textarea></pre>";
STOPTHISCRAZYTHING;
echo $all;
?>
I believe what you are trying to do, you would be able to accomplish by using ob_start() and friends. heredoc (<<<) is simply for creating large string variables without using quotes. you cannot put code and all that stuff inside of a heredoc string.
http://php.net/manual/en/function.ob-start.php
ob_start() can let you buffer everything you print. you can then use the other ob functions to get that buffered output and place it into a variable or whatever you want to do with it.
You've got some interesting bracketing going on there....
Do you need to use Heredoc
? Why not just add it all into a variable using the "." operator to concatenate them. Also to simplify things you can use a mixture of single and double quotes:
$all = "<br><br><textarea rows='30' cols = '100'>"; $all = $all . "<div align='center'><font size='7'>I Have</font></div>"; foreach($same as $match) { $all = $all . "<img src='" . $match . "'>"; } $all = $all . "<div align='center'><font size='7'>I Need</font></div>"; foreach($different as $diff) { if(!in_array($diff, $reject)) { $all = $all . "<img src='" . $diff . "'>"; $all = $all . "<div align='center'><font size='7'>I Am Unable To Obtain</font></div>"; foreach($retired_different as $unabletoget) { $all = $all . "<img src='" . $unabletoget . "'>"; } $all = $all . "</textarea>";
Now when you echo the $all variable you will get the full output you desire
echo $all;