【问题标题】:Codeigniter character_limiter and strip_tagsCodeigniter character_limiter 和 strip_tags
【发布时间】:2012-04-11 14:36:17
【问题描述】:

使用数据库显示带有 html 字符的文章:

$row->文章:

<div class="article">
As is usual also in Buenos Aires, is held in that city again the official Asterisk Advanced course with the new agenda <a href="http://www.google.com">google page</a>
</div>

使用:character_limiter($row->article, 160) 短文本,但碰巧剪掉了超链接,导致html代码出错,我用函数strip_tags()解决...但它显示以下:

<div class="article">
    As is usual also in Buenos Aires, is held in that city again the official Asterisk Advanced course with the new agendagoogle page...
    </div>

如您所见,该函数删除了超链接标签,但也删除了周围的空格。

我已经阅读了这篇文章:Problem using strip_tags in php

...但使用设置为 2 个文本元素的正则表达式。我想知道您是否可以使用类似但带有未定义标签的东西。我希望你的帮助

【问题讨论】:

    标签: codeigniter strip-tags character-limit


    【解决方案1】:

    您不应该有您在示例中显示的内容,因为您在 &lt;a&gt; 开始标记和字符串之间有一个空格。您链接到的这个问题只会在您的用户添加&lt;div&gt; 或类似内容时表现出来。诸如&lt;b&gt;&lt;a&gt;&lt;u&gt; 之类的东西;当有人打字时会有自然的空间。

    你需要做的是:

    echo character_limiter(strip_tags($row->article), 160);
    
    1. 先剥离标签
    2. 限制第二个字符。

    不要担心奇怪的“空格”问题,如果他们没有在the brown fox&lt;a href="#"&gt;jumps over&lt;/a&gt; the..之类的东西之间留出空格,那无论如何都是人为错误的结果。

    HTML 的替代方法是也使用降价代码(您在 SO 上使用什么来输入问题/答案)。恕我直言,它非常干净。

    【讨论】:

    • 在我的网站上,只需使用 character_limiter (strip_tags ($ row-> article), 160);正如您在第一个示例中看到的那样,我认为没有人为错误(在这种情况下),文本与超链接标签分开。
    • @user1151980,在您的示例中,您不应该让strip_tags 删除 ` ` 之前的空格,因为strip_tags 只删除 HTML 标签,而不是空格在他们面前。
    • 奇怪...由于某种原因,这些字符串被加入了。在 wordpress 论坛中找到了一个解决方案: preg_replace('/]*>/','', $str);在使用该功能之前。
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多