How to use php echo without quotes to echo html code? - php

I'm trying to print out some html code which isn't stored as a simple string so I need to decode it before echo-ing, my problem is when I echo a decoded value I keep getting these quotes and they are ruining the output, this is how it looks:
<div>
"<h1 class="ql-align-center">TEST</h1>"
</div>
so because of these quotes " h1 failes to form and it prints out as a text and not as an html code.
So i'm wondering is it possible to print as html code meaning there is no quotes "" ?
this is the php code that it generates it like this
<?php echo html_entity_decode($singleEmail['camp_desc'], ENT_NOQUOTES, 'UTF-8'); ?>
also this is the database value of 'camp_desc' that has to be encoded before being printed
&lt;h1 class=&quot;ql-align-center&quot;&gt;TEST&lt;/h1&gt;
and the output of php code above for encode is
<h1 class="ql-align-center">TEST</h1>
but since I'm using echo to print.... php wraps it with quotes and <h1> tag becomes a plain text instead of html element

I don't know where the quotes are coming from - the code you have in your question doesn't add extra quotes so they are coming from somewhere else.
However if you want the HTML string to be rendered as HTML instead of displaying the tags as text, you can do the following:
Starting with this value in your variable:
&lt;h1 class=&quot;ql-align-center&quot;&gt;TEST&lt;/h1&gt;
displayed as: <h1 class="ql-align-center">TEST</h1>
...you can use html_entity_decode to decode it which will give us the following output, i.e. it converts it into a string that will display as plain text HTML when you echo it:
<h1 class="ql-align-center">TEST</h1>
displayed as: <h1 class="ql-align-center">TEST</h1>
...now we need to decode this to turn it into the HTML elements that will be displayed as a H1 tag in the page:
<h1 class="ql-align-center">TEST</h1>
displayed as: TEST
Code: To do this, you need to call html_entity_decode twice before it will display the string as HTML elements:
<?php
$htmlstr = html_entity_decode($singleEmail['camp_desc'], ENT_QUOTES, 'UTF-8');
echo html_entity_decode($htmlstr, ENT_NOQUOTES, 'UTF-8');
?>

What if you try to replace those quotes when echo-ing them?
Like you make regular expression to replace it or you make a function that replaces the two parts you want like str_replace('”<', '<', $yourDecodedHtml),
str_replace('>”', '>', $yourDecodedHtml)

Related

Remove Tags from string, Stored from ckeditor

I'm trying to echo value in PHP, which is stored in database using ckeditor, the value stored in database is something like
<p>sample text</p>
now im printing value using
<?php echo $row[0]->content; ?>
but the output I'm getting is
<p>sample text</p>
how to remove p tags from string?
String <p>sample text</p> includes some encoded html entities (< for < and > for >).
If you want to replace this encoded entitites with real symbols - use html_entity_decode. After it, your string becomes <p>sample text</p>. So if you echo it, <p> and </p> will be considered html-tags and will not be shown.
If you still need to remove these tags - use strip_tags function.
In the end:
<?php echo strip_tags(html_entity_decode($row[0]->content)); ?>
And yes, strip_tags removes all tags from a string unless you use it's second parameter.

Stuck with strip_tags why not working in my code?

I want to remove quotes and html tags from following string:
$mystring='Example string "I want to remove any html tags ABC<sub>DE</sub> from <p>similar</p> types of string?"';
I am using following scripts to remove it but it's doesn't work for me:
echo strip_tags(htmlentities($mystring,ENT_QUOTES));
I want to following output for above string:
Example string "I want to remove any html tags ABCDE from similar types of string?"
Why strip is not working?What I mess here?
Once you use htmlentities() on that string there are no tags left to strip because they have been converted, well, to their HTML entities.
At first use strip_tags function if you want remove html tags then use htmlentities as follow it should be work:
echo htmlentities(strip_tags($mystring),ENT_QUOTES);
Can you use this code
echo strip_tags($mystring);

nl2br doesn't work with htmlspecialchars

So i'm trying to get my nl2br to work but it just wont work the way i hope it would.
As it retrieves text from a database, i want it to recognise the \n with the nl2br I have htmlspecialchars which is the reason it doesn't work. How would i solve this issue?
My code:
The post variable looks like this
$post = nl2br($row['p_post']);
Which means that it ends up inside the htmlspecialchars()
echo '<p>'.htmlspecialchars($post, ENT_QUOTES).'</p>;
I tried to put it like this echo '<p>'.nl2br(htmlspecialchars($post, ENT_QUOTES)).'</p>; but that ended up looking as if there was no nl2br at all.
How it looks from the start
How it looks after i've added the nl2br
You're double encoding the tags:
$foo = "how\ndoes\nthis\nlook";
$post = nl2br($foo); // $post is now "how<br>does<br>this<br>look"
$temp = nl2br(htmlspecialchars($post)); // $temp is now "how<br>does etc..."
echo '<p>how<br>does< etc...';
You converted the line breaks to break tags, then you html encoded the break tags to html character entities, so your browser renders those tags as "visual" tags, and not HTML line breaks at all.
This was fixed, it seemed that another variable collided with it somehow so that it wouldn't actually make the <br> tags.

How to join variables in PHP

How can I join '<' and 'div>'?
When I echo
<?php echo "<"."div>";?>
it returns an empty string.
Because it is a HTML Tag. You cannot print it like this.
Still if you want to print it you can see #egig's suggestion. You can use echo "&lt"."div>"
That's because you're echoing a HTML tag. View the source of the page.
use:
htmlspecialchars("<a href='test'>Test</a>");
to display html tags in php
You can use htmlspecialchars for this.
<?php echo htmlspecialchars("<"."div>"); ?>
Your code is executed and the '<' is joined with the 'div>', however there is nothing to show as you are trying to output a partial HTML tag in PHP. You can do this by means of entity code:
< Less-than <
> Greater-than >
In your case it looks like:
echo "<"."div>";
Other means of doing this is using PHP to add these entity codes for you:
htmlentities — convert all applicable characters to HTML entities
In code:
echo htmlentities('<div>');
Make sure you use echo, or otherwise you would end up with an empty string again.
Other means:
htmlspecialchars — convert special characters to HTML entities
In code:
echo htmlspecialchars('<div>');

Displaying HTML code

How can stop HTML code from rendering and display it as standard text, so my users can simply copy and paste it for their own usage?
You could use the function htmlentities(). Have a look at the manual.
Please note that by default the data will be returned using the ISO-8859-1 character set. You might want to change the third parameter to UTF-8.
For example:
$html = "<div>Some text</div>";
echo htmlentities($html);
Use PHP to set the content type:
header("Content-Type: text/plain");
You parse it into markup which uses HTML entities, for example instead of using:
<body>
you would instead use entity codes to escape the markup characters, like this:
<body>
There are plenty of references to be found which list entity codes, and it's pretty easy to parse and escape HTML in most programming languages.
HTML code can be displayed using php script by using functions htmlentities() and htmlspecialchars(). For understanding this concept consider the following example:
<?php
$a="<h1>heading tag: used for heading tags</h1>";
echo htmlentities($a)."<br/>";
echo htmlspecialchars($a);
?>
Output will be:
<h1>heading tag: used for heading tags</h1>
<h1>heading tag: used for heading tags</h1>

Categories