本文介紹了如何使用 PHP 在白名單中允許 HTML的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我知道多年來有很多關于使用 PHP 過濾數據的最佳方法的討論,但我想在我當前的項目中采用白名單方法.
我只希望用戶能夠使用以下 HTML
粗體<i>斜體</i><u>下劃線</u><s>刪除線</s><大>大尺寸</大><小>小尺寸</小>超鏈接<a >網站</a>項目符號列表:<ul><li>一個項目</li><li>另一個項目</li>有序列表:<ol><li>第一項<li>第二項</ol><blockquote>因為它是縮進的</blockquote><h1>標題 1</h1><h2>標題2</h2><h3>標題3</h3>
任何人都可以向我展示在 PHP 中執行此操作的最佳方法嗎?我過去只允許所有 html 減去某些代碼
解決方案
最簡單的解決方案是 strip_tags(),
它接受包含允許標簽的第二個參數:
strip_tags($string, "
I know there is a lot of discussion for years on best methods of filtering data with PHP but I would like to go the whitelist approach in my current project.
I only want a user to be able to use the following HTML
<b>bold</b>
<i>italics</i>
<u>underline</u>
<s>strikethrough</s>
<big>Big size</big >
<small>Small size</small>
Hyperlink <a >website</a>
A Bulleted List:
<ul>
<li>One Item</li>
<li>Another Item</li>
</ul>
An Ordered List:
<ol>
<li> First Item</li>
<li> Second Item</li>
</ol>
<blockquote>Because it is indented</blockquote>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
Can anyone show me the best method of doing this for performance in PHP? I have only in the past allowed all html minus certain codes
解決方案
The simplest solution would be strip_tags(),
which accepts a second argument containing allowable tags:
strip_tags($string, "<b><i><u><a><s><big><small><ul><li><ol><blockquote><h1><h2><h3>");
這篇關于如何使用 PHP 在白名單中允許 HTML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!