Best way to sanitize content with PHP?

Posted by Jens Törnell on Stack Overflow See other posts from Stack Overflow or by Jens Törnell
Published on 2010-03-21T14:11:07Z Indexed on 2010/03/21 14:21 UTC
Read the original article Hit count: 582

Filed under:
|
|
|

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;
}

© Stack Overflow or respective owner

Related posts about php

Related posts about string-manipulation