簡體   English   中英

將純文本制作為HTML,但僅鏈接?

[英]Make plain text to HTML but only links?

該方案是:

  1. 用戶將很多內容放在保存到數據庫的文本區域中。
  2. 通過使用htmlentities()保存的內容是“確保純文本”。

當輸出到網頁時,它顯示為這樣(鏈接也顯示為純文本)

Hello there, how are you today. 
This text is saved and I want to show links, 
but the links are displayed like this: <a href="text">text</a>.

(because the html surrounding the link are &lt; and &gt;)

之所以這樣(僅純文本)是因為我不希望用戶使用HTML標簽等弄亂內容。

是否可以替換&gt; &lt; 進入<和> 但僅當存在鏈接時 我不確定如何實現這一目標。

更新:可能正在使用自定義標記,例如[mylink]actual link[/mylink] 也可以。 這可能是更好的解決方案? 你怎么看?

不要使用htmlentities而是使用strip_tags

strip_tags —從字符串中剝離HTML和PHP標簽

有一個選項可以忽略某些標簽。 在您的示例中:

$text="This <b>is</b> a <a href='#'>test</a>";
echo strip_tags($text,'<a>');

這將除去錨點之外的所有HTML標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM