Page 1 of 1

[FIXED] O'Brien

Posted: Sun Jan 25, 2009 9:51 pm
by xeorex
How to sanitise a name such as O'Brien and be able to show it back on the form without being shown as O\'Brien?

Code: Select all

<?php
 
    function html_prepare($html) {
                     trim(htmlentities($html, ENT_QUOTES));
                     return $html;
                 }
    function validate_name($input) 
        {       
              $pattern = "/^[\' a-z]{2,20}$/i";
                      return (bool)preg_match($pattern, $input, $matches);
        }
 
if (validate_name($_POST['name']) == false) {
$error['missing']['name'] = 'Name seems to be invalid';
$html['name'] = html_prepare(ucwords(strtolower($_POST['name'])));
} else {
$clean['name'] = trim(ucwords(strtolower($_POST['name'])));
$html['name'] = html_prepare($clean['name']);
}
 
 
?>
The $html array element is the one that will be shown again if the form is not completed properly.

Re: O'Brien

Posted: Sun Jan 25, 2009 9:54 pm
by John Cartwright
stripslashes(), or better yet turn off magic quotes..

[FIXED] Re: O'Brien

Posted: Sun Jan 25, 2009 9:57 pm
by xeorex
Thank you for your quick reply. :D

Re: O'Brien

Posted: Sun Jan 25, 2009 9:58 pm
by John Cartwright
No problem (even quicker this time ;)).