peter-cooper
asked on
Echo statement rendering html code in output
This is such a basic question that I feel embarrassed asking it. Whenever I echo html tags, instead of rendering the tag, it just displays in the echo output. I have tried single and double quotes, but still no luck. What am I doing wrong here? Thanks
ff rendered output:
<a style="margin-left: 12px;" href="javascript:void(0)" class="tooltip" title="<?php echo "Please select your boxes from the list. You can select a max of 20 boxes per submission.<br />You can select multiple boxes by holding the left ctrl on your keyboard and making your selection"; ?>">Help</a>
ff rendered output:
title="Please select your boxes from the list. You can select a max of 20 boxes per submission.<br />You can select multiple boxes by holding the left ctrl on your keyboard and making your selection"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Producing HTML output is the normal use of echo in this way.
If you want to actually show the HTML tags, you need to surround it with
htmlspecialchars()
which is a php function that changes special characters to their ascii codes.
$x = htmlspecialchars("Here is real HTML <br>");
echo $x;
http://us2.php.net/manual/en/function.htmlspecialchars.php
If you want to actually show the HTML tags, you need to surround it with
htmlspecialchars()
which is a php function that changes special characters to their ascii codes.
$x = htmlspecialchars("Here is real HTML <br>");
echo $x;
http://us2.php.net/manual/en/function.htmlspecialchars.php
The "deep background" on PHP's use of quotes:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
The code snippet, copied and pasted from the original post is here:
http://iconoun.com/demo/petercooper.php
It does not do anything odd, so I would surmise that there is something else afoot. Can you please create the SSCCE that enables us to reproduce the problem?
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
The code snippet, copied and pasted from the original post is here:
http://iconoun.com/demo/petercooper.php
It does not do anything odd, so I would surmise that there is something else afoot. Can you please create the SSCCE that enables us to reproduce the problem?
No SSCCE needed he is outputing HTML tags into a title string which is agent dependent on how it is rendered - however the default is to render it as text - not as html - therefore the <br/> will be treated as text and not interpreted as a line break.
Solution is to insert a line break directly into the text.
Solution is to insert a line break directly into the text.
@julianH: I understood that ff rendered output: meant "Firefox rendered output:" and so I copied the code snippet, installed it on my server and visited it in Firefox. The URL on my server is here:
http://iconoun.com/demo/petercooper.php
A screenshot of the rendered document:Since this did not look like the "ff rendered output" in the question, there is some disconnect in our understandings of the issue. Ergo, I asked for a test case that was able to demonstrate the issue.
http://iconoun.com/demo/petercooper.php
A screenshot of the rendered document:Since this did not look like the "ff rendered output" in the question, there is some disconnect in our understandings of the issue. Ergo, I asked for a test case that was able to demonstrate the issue.
@Ray - looks identical to me - unfortunately your screen shot does not show what happens if you hover over the link - if you did you would see the following
As I understand it the problem is the <br/> appearing in the title popup - which was demonstrated by the author in his second code snippet of his original post.
As I understand it the problem is the <br/> appearing in the title popup - which was demonstrated by the author in his second code snippet of his original post.
Ahh, I see now. If that's the real issue, the correct answer is right at the top:
https://www.experts-exchange.com/questions/28531936/Echo-statement-rendering-html-code-in-output.html?anchorAnswerId=40363461#a40363461
But now that I understand the question, this has nothing to do with PHP and echo. In fact, PHP is not needed to render the title attribute. Just put in the newline character directly. Easy!
https://www.experts-exchange.com/questions/28531936/Echo-statement-rendering-html-code-in-output.html?anchorAnswerId=40363461#a40363461
But now that I understand the question, this has nothing to do with PHP and echo. In fact, PHP is not needed to render the title attribute. Just put in the newline character directly. Easy!
<a style="margin-left: 12px;"
href="javascript:void(0)"
class="tooltip"
title="Please select your boxes from the list.
You can select a max of 20 boxes per submission.
You can select
multiple boxes by holding the left ctrl on your keyboard
and making your selection">Help</a>
ASKER
Thanks for comments guys. However, neither the \n method or the 
 method work. They do not echo, but they do not put the text on a new line, like a <br /> tag would. Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please recheck your code carefully, then post the exact URL of your test case that demonstrates the issue. I can see no reason why our demonstration scripts and test cases should produce different output from your demonstration script. This is why we ask for the SSCCE -- it disambiguates so many things that can be lost in explanations.
ASKER
I have included screenshot where it is not putting the text on a new line. I think perhaps it could have something to do with a jquery plugin I am using, tooltipster. Should have thought of that in the first place. If I remove the class then it displays correctly as a title with cr and lf etc. But not with tooltipster. I shall report back. Thanks
ASKER
Should have checked this first. Tooltipster has a method, contentAsHTML: true where you can include html in title tag. If it is ok, I shall split the points with Julian and Lukasz. Thanks
ASKER
Thanks very much for help.
E-E has a jQuery Zone. It would probably have made for a quicker solution if you had told us you were using a jQuery plug-in, and what the plug-in was called and how you were using it. It's this sort of thing that we seek to understand when we ask you for the SSCCE because while we are experts, we are not mind readers and we need your fullest explanation and exposition to discern the important details in your question.
ASKER
Thanks Ray for your comment. You are absolutely right in what you say. I should have realized that and had I done so, would not have needed to post at all :-). Thanks for input.
Yeah, I get it. As Kettering said, "A problem well stated is a problem half solved!"
Open in new window