将纯文本设为 HTML 但只有链接?

Make plain text to HTML but only links?

场景是:

  1. 用户在保存到数据库的文本区域中放置了很多内容。
  2. 使用htmlentities()保存的内容是"ensure plain text"。

并且当输出到网页时它显示如下(并且 link 也显示为纯文本)

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;替换为<和>但只有当有link时才可以?我不确定如何实现。

更新: 一个想法可能正在使用像 [mylink]actual link[/mylink] 这样的自定义标签。也可以。这可能是更好的解决方案?你有什么想法?

而不是使用 htmlentities 使用 strip_tags:

strip_tags — Strip HTML and PHP tags from a string

有一个选项可以忽略某些标签。在你的例子中:

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

这将删除每个 HTML 标签,但锚点除外。