简体   繁体   中英

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

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

&amp;lt;h1 class=&amp;quot;ql-align-center&amp;quot;&amp;gt;TEST&amp;lt;/h1&amp;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:

&amp;lt;h1 class=&amp;quot;ql-align-center&amp;quot;&amp;gt;TEST&amp;lt;/h1&amp;gt;

displayed as: &lt;h1 class=&quot;ql-align-center&quot;&gt;TEST&lt;/h1&gt;

...you can use html_entity_decode to decode it which will give us the following output, ie it converts it into a string that will display as plain text HTML when you echo it:

&lt;h1 class=&quot;ql-align-center&quot;&gt;TEST&lt;/h1&gt;

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM