|
|
|
|
![]() ![]() |
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 |
|
|
|
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 ) |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 8th September 2008 - 09:18 AM |