Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

php - How Convert HTML content and Special Characters to Html entity

I am working with phpOffice/phpword document download. When I pass the content to the document download the content is like this,

<p>& operation check</p><p> < operation check</p><p> > operation check</p>

I want to change special character and content like,

<p> &amp; operation check</p> <p> &lt; operation check</p> <p> &gt; operation check</p>

I have done;

$textContent = strip_tags($content);
$content = htmlspecialchars($textContent);

The issue for the above I can replace html entities for the special characters, but I remove the html other tags and pass to document creation. then content style missing because only send the text content.

I have check the php - tidy. but not worked properly. can someone explain way to implement above operation.

question from:https://stackoverflow.com/questions/65842187/how-convert-html-content-and-special-characters-to-html-entity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This doesn't really make sense. The whole point of escaping characters in HTML is to do it before you add the tags. Something like this:

$content = "<p>" . htmlspecialchars("& operation check") . "</p><p>" . htmlspecialchars("< operation check") . "</p>";

If the data you have is already not escaped, then that data is broken, and shouldn't be used. You'll need to go back one step, and find out where you getting the data from and why is it broken, and get the data before it is broken.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...