Clean Up User-Input & Convert Line-Breaks To BR-tags

A simple PHP function to clean up user input.
It does following:

  • converts & to &
  • converts < to &lt;
  • converts > to &gt;
  • converts to &quot;
  • and converts line-breaks to <br />

[ad name=”Adsense – text only”]

The Function:

function clean($str) {
    $str = preg_replace('/&(?!#[0-9]+;)/s', '&amp;', $str);
    $str = str_replace(array('< ', '>', '"'), array('&lt;', '&gt;', '"'), $str);
    $str = nl2br($str);
    return $str;
}

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *