Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Cleaning User Input With Regex
kvarnerexpress
post Sep 21 2005, 09:51 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



As anyone who works with user input knows, not everyone who submits information makes it look proper.

One one of my web forms, I parse all the needed fields that I wish to be title cased; their name, address, city, etc.

I use this to perform this action, which works nicely:

PHP Code:

CODE
$string = ucwords(strtolower($string));


This fixes the input if the user types in all caps or all lowercase. There is a problem I have been noticing about those who enter a generation after their name, such as "Bill Warner III".

The above function changes it to "Bill Warner Iii", which is incorrect. I have been toying with a regular expression to catch these I's (or sometimes V's) and turn them all uppercase, but to no avail. Here are versions of my non-working code:

PHP Code:


CODE
$string = preg_replace("/\b([IiVv])+\b/e", strtoupper("$1"), $string);

$string = preg_replace("/\b([IiVv]+)\b/e", strtoupper("$1"), $string);

$string = preg_replace("/\b([IiVv]+)$/e", strtoupper("$1"), $string)



Thanks,kivarnerexpress
Go to the top of the page
 
+Quote Post
Spectre
post Sep 22 2005, 08:11 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



This pattern should match almost any Roman numeral below L/50 (it is unlikely you will ever encounter anything beyond, say, 10th/X generation names).

CODE
/\b(i+|i+[vx]*?|v[i]*?|x+[vi]*?)\b/ei


Here's a quick example (the names are, of course, randomly chosen). Note that the 'strtoupper()' function is being evaluated as PHP code and is enclosed in quotes.

CODE
<?php
$names = array(
  'john sMith iv',
  'Richard jones Iiv',
  'JOE NOBODY II',
  'Viva vivi ixen ivan III',
  'Person XI',
  'person xv'
);
$pattern = '/\b(i+|i+[vx]*?|v[i]*?|x+[vi]*?)\b/ei';
for( $i=0;$i<count($names);$i++ ) {
  $name = ucwords(strtolower($names[$i]));
  $string = preg_replace($pattern, 'strtoupper("$1")', $name);
  $names_p[$names[$i]] = $string;
}
print_r($names_p);
?>


Outputs:
QUOTE
Array
(
    [john sMith iv] => John Smith IV
    [Richard jones Iiv] => Richard Jones IIV
    [JOE NOBODY II] => Joe Nobody II
    [Viva vivi ixen ivan III] => Viva Vivi Ixen Ivan III
    [Person XI] => Person XI
    [person xv] => Person XV
)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Php Login Script(18)
  2. User Login System With Setcookies(13)
  3. Password Strength / User Availablity Scripts ?(2)
  4. Script That Tracks The User Status(4)
  5. Securing A Php Script Proccessing Input(7)
  6. [^] Need Help With Regex (for .htaccess)(2)
  7. How Good Is This Data Cleaning Function?(2)
  8. Directing To A User To Specific Page First Time Only(3)
  9. Multiple Drop Down Lists ?(4)
  10. Compare 2 List Of User Ids From Different Tables(1)
  11. Php An Js Window.open Pages Trouble.(3)
  12. [mysql]get Id Of Loged In User?(7)
  13. Php Ftp Upload Form(1)
  14. Unexpected T_string In User.php [resolved](5)


 



- Lo-Fi Version Time is now: 8th September 2008 - 09:18 AM