Jul 24, 2008

Cleaning User Input With Regex

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Cleaning User Input With Regex

kvarnerexpress
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

 

 

 


Reply

Spectre
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
)

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : cleaning user input regex

  1. Unexpected T_string In User.php [resolved] - (5)
  2. How Good Is This Data Cleaning Function? - (2)
    Hi all, this is my first function and as part of a script and i just want to know a couple of
    things. here is the code for the function: CODE <? function
    clean($dirty_string) { $muddy_string = stripslashes($dirty_string);
    $murky_string = strip_tags($muddy_string); $clean_string =
    htmlentities($murky_string);      }; ?> So the first thing is how secure is
    it? the script this will be used in connects to a database and sends an email so it needs to stop
    SQL injections and any email ab...
  3. Php Ftp Upload Form - Adding User Directory to PHP Upload Form - Help (0)
    Alright I am trying to have a PHP FTP Upload Form that allows the user to create the directory
    folder for where they want to upload there files to. example: Main Directory: vainsoft.com There
    directory: vainsoft.com/modeling or vainsoft.com/photography But I dont want them to be able to
    upload things into the main directory, only sub-directories, is that possible with this coding that
    I have: //uses $_FILES global array //see manual for older PHP version info //This
    function will be used to get the extension from the filename function get_extension($fi...
  4. Script That Tracks The User Status - how can I track on or offline users? (4)
    long explaination: hey, I'm building a user profile site right now. And, I kinda know how to
    make a online/offline detector, but not totally sure. I know I can make a mysql database to track
    them, but how does it entrer the information? I could easily put in a field where when they login it
    sets them to online, but if they don't sign out, and just exit the browser, how can I tell.
    short: I want someone to tell me how to make a online/offline status detector, like they have here
    on trap17. I'd be thrilled if you can post to this, thanks, arcticsnpr...
  5. [mysql]get Id Of Loged In User? - (7)
    how to get the id number of the loged in user? my db is id. username. password. i have tryed a
    few things.. but i never seem to get it right /ohmy.gif" style="vertical-align:middle" emoid=":o"
    border="0" alt="ohmy.gif" />...
  6. Php An Js Window.open Pages Trouble. - I need a way to set hidden input values to the new window. (3)
    I have 2 main pages Page A(events_locked.php) and Page B(add_attendance.php). Both are php files.
    Page A takes a post var from another page(not Page B ) and then used to query for displaying records
    in a mysql datase. This variable has set as a session variable because there is 1 <script
    LANGUAGE="JavaScript"> window.name="main_index"; function openFormWindow() {
    OpenWindow=window.open("add_attendance.php", "newwin", "height=250,
    width=400,toolbar=no,scrollbars=no,menubar=no,location=no,resizable=no"); var x =
    getElementByName("form1"); x.target="newwin"; x.s...
  7. Compare 2 List Of User Ids From Different Tables - (1)
    Hi all. I am trying to make a list out of 2 list. The first list is a complete list of users id
    from users table (List A) The Second list is of users id from another table (List /cool.gif"
    style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> I want to subtrack the user
    ids in List B from List A and make List C. Thanks in advance for any help /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Update: I looked up a old
    sql book i had and found some query examples. I tried the EXCEPT statement in mysql but it dont wor...
  8. Multiple Drop Down Lists ? - Multiple drop down lists to take user to new page (4)
    Hi everyone I was wondering if anyone could help. I want to create a page with multiple drop down
    lists and depending what the user selects decides the page they will be taken to. Sorry i havent
    explained this too well. Here is an example of what i want (link below) the user is also emailed a
    copy http://www.dermalogica.com/SpeedMappingOnl...US®ion=B I have searched the web and come
    close but nothing does it right I would be extremely greatful if anyone could help! Thanks ...
  9. Directing To A User To Specific Page First Time Only - (3)
    I make a signup page. I want that when someone signups and login for first time..then he is directed
    to the page X automatically. but after that it is redirected to Page Y always... Its pretty simple
    thing to do but im not understanding how i can make the script to identify that a person has logged
    in for first time ... anyhelp would be so welcome. Thanks....
  10. [^] Need Help With Regex (for .htaccess) - (2)
    Well, I ran into a problem. I tried to make a code that matched everything, except strings
    containing the word "php", "gif", "jpg" etc. I tried doing something like this, but it didn't
    work: CODE ([^\b(php|gif|jpg)\b]+)$ But that works
    just like if I would do this: CODE ([^phgifj]+)$ That code is a part of
    a larger code that only works partially: CODE RewriteEngine on RewriteRule  
    ([^\b(php|gif|jpg)\b]+)$    item.php?n=$1 Some
    strings, su...
  11. Securing A Php Script Proccessing Input - I need help and advice (7)
    Hi everyone I like to experiment with php, been doing so for about a year but im not clued up on
    security yet, i can use str_replace() to take out parts of strings i dont want like html and JS code
    inserted by users, and i know how to use stripslashes to take out slashes from input strings but
    this doesnt seem to be lock-tight security to me. There has to be something more to protect my
    scripts against malicious users. I belive its possible to use the "or_die()" function for
    connecting to an sql table to prevent the error messages being shown to users, as error message...
  12. Password Strength / User Availablity Scripts ? - (2)
    many of u guys would already have noticed that now a days , on most of the websites , when some one
    sign in...as he puts his desired username in the textbox , the page shows that it is not available
    or it is available...without pushing any button etc.. and the second thing , when some one writes
    the password , on the same screen , it shows that password is weak or password is strong... i think
    it is done with php ... can some one give me a link to any page where i can access these scripts ?
    or some one can help me regarding this ?? Thanks for helping always....
  13. User Login System With Setcookies - (13)
    a friend of mine is quite good at php and told me not to use sessions and to use setcookie im not
    sure how to use setcookie to make a user authentication system and was wondering if anyone here know
    a tutorial on how to do it...
  14. Php Login Script - Removing the login field once the user logs in (18)
    Hey everyone. I have a login script that i know works. My question is that on my main page, it have
    a form to allow the user to log in. What i want it to do is that once the user logs in, the form
    disappears and the users data (aka username) is displayed where the form was. At the moment i cant
    get it to work. Below is my code. CODE <div id="loginMenu">  <?php    if
    ($logged_in == 1)    {  ?>     <!-- User information -->  <?php
        }//if     else     {  ?>     <form action="<?php echo &...
  15. Create A Windows User Account - (1)
    hi, i am making an online registration scheme for my website for some webspace. i have got every
    thing sorted out exsept for one thing. to acces the ftp service and upload files to there user space
    i need to create a user account on the server for that user. is there a way to create a user account
    on a windows operating system with php 5 installed. thanks, kvarnerexpress ...
  16. User Log-in - PHP question (2)
    Hello all, I am a PHP n00b. I have read the Login page post, which is over my head. I was hoping
    someone could explain a concept to me more in layman's terms. This is what I hope to do - I want
    to go to my url (www.xyzblah.com) and have it automatically prompt for either a username and
    password, or just a password. After the correct credentials are supplied, go to the regular
    index.html. Any help you could give me is appreciated. Thanks....



Looking for cleaning, user, input, regex

Searching Video's for cleaning, user, input, regex
advertisement



Cleaning User Input With Regex



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE