I want to print some html code which i stored in database, when ever i try to print that variable through its returning the compiled result of HTML code.
CODE SNIP :
$html = '<html><body><h1>hello</h1></body></html>'(this one store in db)
echo $html;
OUTPUT:
hello in h1 format
But I want
'<html><body><h1>hello</h1></body></html>' this as output.
Can anyone help me on this.
Instead
echo $html;
Use that
echo htmlspecialchars($html);
Use this
echo htmlspecialchars($html);
Related
<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".
I am testing this on windows 7 xammp 1.8.1 php 5.4.7
I am trying to show dynamic php codes in html as example
my code is
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
but output html is blank! i am not sure if its a bug, can some help me.thanks in advance
When you run this, this DOES produce an output, which is a blank page, because the output is:
<?php echo ?>
To a browser which renders html, it will look like an open tag with nothing in value.
Run your script and view the page source...
You need to use single quotes
i.e.
change
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
to
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Just change your double quotes to single quotes:
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Example: http://ideone.com/bUJAxb
There are two things you have to change. First, if you use double quotes PHP will evaluate variables in it, so your output will be <?php echo ?>:
$output='<?php echo $ti ?>';
Now the output will be <?php echo $ti ?>.
Next, the browser will interpret this as HTML and since it is only a single tag it will display nothing. You need to run this through htmlentities():
echo htmlentities($output);
This will output <? echo $ti ?;gt; which will be displayed by the browser in the way you intend it.
You need to escape the characters
This can be done by adding the entire line you want to output in an htmlentities function call, like:
$output = htmlentities("<?php echo \$ti ?>");
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText
I have got a string contain with html tags.I want to print it using php , but it print with html tags.How to print somethings like this?
String :
<p style="color:#f00">HTML basic test Text</p>
Want to show :
HTML basic test Text
currently I show the result : <p style="color:#f00">HTML basic test Text</p>
Have you any solution?
Like CBroe said, you can use strip_tags() function
<?php
$text = '<p style="color:#f00">HTML basic test Text</p>';
echo strip_tags($text);
?>
AFAICT an echo should do it. If I need some HTML code to be generatet with php I echo it like:
<?php
echo '<p>Hallo world</p>';
?>
Worked for me, I never used print for that purpose.
PHP echo function is not outputing strings in the format of html tag like <something. somethong_else>, may be because it is like HTML tags, is there any way to display it?
echo 'hi<h.i>';
Eg : this displays as
echo 'hi';
try using
<?php
echo htmlentities('hi<h.i>');
?>
You need to encode the string if you what the text to appear
echo htmlentities("hi<h.i>");
There is a thing called HTML. Where strings in <something.somethong_else> format have some meaning. Go figure.
PHP can echo out tags.
Example
<?php
echo '<p>Hello World</p>';
?>
Keep in mind, the PHP will echo where it is called. So you can also do this
<p>
<?php echo 'Hello World'; ?>
</p>
UPDATE
Since new information is sent. You can make < into < and > into > Look at HTML entities.
You probably need to use htmlentities(), try this:
echo htmlentities('hi<h.i>');