用PHP清理内容的最佳方式是什么? [英] Best way to sanitize content with PHP?

查看:131
本文介绍了用PHP清理内容的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些是净化内容的最佳方式?一个例子...

示例 - 在清理之前:

  Morbi mollis ante vitae massa suscipit a tempus est pellentesque。 Pellentesque栖息地morbi tristique senectus et netus et malesuada fames ac turpis egestas。 Nulla mattis iaculis consectetur。 
Morbi mollis ante vitae est pellentesque。 Pellentesque栖息地morbi tristique senectus et netus et malesuada fames ac turpis egestas。 Nulla mattis iaculis consectetur。

示例 - 清理完毕后

 < p> Morbi mollis ante vitae massa suscipit a tempus est pellentesque。 Pellentesque栖息地morbi tristique senectus et netus et malesuada fames ac turpis egestas。 Nulla mattis iaculis consectetur。< / p> 

< p> Morbi mollis ante vitae est pellentesque。 Pellentesque栖息地morbi tristique senectus et netus et malesuada fames ac turpis egestas。 Nulla mattis iaculis consectetur。< / p>

应该做什么




  • 它应该添加p-tags而不是换行符。

  • 它应该删除空格,例如三位空格。
  • 它应该删除换行符。

  • 它应该删除制表符。

  • 它应该在内容前删除换行符和空格。


  • 我使用 str_replace 函数,它应该是一个更好的解决方案吗?



    我想要函数看起来像这样:

     函数sanitize($ content)
    {
    //做魔术!
    返回$ content;


    解决方案


    • 它应该添加p-tags而不是换行符。



    通过类似Textile解释器或Markdown另一个人道标记语言它适合您的需求。




    • 它应该删除空白空间,如tripple空格

    • 换行符

    • 它应该删除标签。

    • 它应该在内容前删除换行符和空格。 b
    • 它应该在内容后面删除换行符和空格(如果有)。



    为什么要麻烦?当HTML呈现为文档时,多个空格字符会被缩减为一个空格,不是?你的大部分问题都会解决。

    Which is the best way to "sanitize" content? An example...

    Example - Before sanitize:

    Morbi mollis ante vitae massa suscipit a tempus est pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla mattis iaculis consectetur.
    Morbi mollis ante vitae est pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla mattis iaculis consectetur.
    

    Example - After sanitize:

    <p>Morbi mollis ante vitae massa suscipit a tempus est pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla mattis iaculis consectetur.</p>
    
    <p>Morbi mollis ante vitae est pellentesque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla mattis iaculis consectetur.</p>
    

    What it should do

    • It should add p-tags instead of line break like.
    • It should remove empty space like tripple spaces
    • It should remove double line breaks.
    • It should remove tabs.
    • It should remove line breaks and spaces before the content if any.
    • It should remove line breaks and spaces after the content if any.

    Right know I use the str_replace function and it should be a better solution for this?

    I want the function to look like this:

    function sanitize($content)
    {
        // Do the magic!
        return $content;
    }
    

    解决方案

    • It should add p-tags instead of line break like.

    Run it through something like the Textile interpreter, or Markdown, or any another humane markup language which suits your needs.

    • It should remove empty space like tripple spaces
    • It should remove double line breaks.
    • It should remove tabs.
    • It should remove line breaks and spaces before the content if any.
    • It should remove line breaks and spaces after the content if any.

    Why bother? When HTML is rendered as a document, multiple white space characters are reduced to a single space, no? Most of your problems solve themselves.

    这篇关于用PHP清理内容的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆