0

I am requesting following format from user

KEYWORD <name> , <city> , <24:00 hour time>

Point 5 is most important, I want to make sure I parse <24:00 hour time> in all possible ways that I can.
What I did/want
1. Split the input to get $info (= $input - $keyword) as follows:

$key = 'abc'; $msg = 'ABC blah, blah, blah';
$pat = "/^(?i)$key/";
$split = preg_split($pat, $msg);
$info = $split[1]; //$split[0] = ''; //empty string

2. My user is dumb. S/he will be "texting (SMS)" the input. So I want to split $info by char , (which hopefully will occur only twice) and store the three substrings into $usName, $usCity, $usTime respectively (prefix us = Un-Safe data, processed data goes into DB)
So I think I will do following

$split = preg_split('/,/', $info);
foeach($split as $block) {
    if(preg_match('/[12]?:\d{2}/', $block) {
        $usTime = $block;
    }
} //what do I do for $usName and $usCity

3. $usName goes directly into DB (don't worry I use prepared stmts)
4. $usCity needs to be mapped to a City in the DB table. I have indexed City_Variance and City_Key tables mapped to each other by City_Variance_City_Key table. So mistyped input can be mapped to proper indexed City name. For now assume that City_Variance has /[:upper:]+/ so no multi word cities (i.e. NOT Los Angeles)
5. This is the most interesting part, the $usTime. The intended use of this field is to create an appointment in nearby future. Found Datejs. This is all PHP server side code, and I don't think I can use the power of a simple JavaScript library without simulating a browser
Does anyone know a library which can help me parse user input like Datejs?

wadkar
  • 960
  • 2
  • 15
  • 29
  • Your question is a little confusing. Could you simply remove the irrelevant parts instead of striking them out? :) – Tomalak Oct 07 '11 at 09:44

1 Answers1

0

If you have

KEYWORD <name> , <city> , <24:00 hour time>

and "KEYWORD" is [:alpha:]+ and the other things cannot contain a comma, just use one regex with sub-groups instead of split().

$re = "/
^
[A-Z]+\s*
([^,]+)\s*,\s*
([^,]+)\s*,\s*
((?:[01]?[0-9]|2[0-3]):[0-5][0-9])
$
/x";

$msg = 'ABC blah, blah, 12:45';

$result=null;

preg_match($re, $msg, $result);

print_r($result);

returns

Array
(
    [0] => ABC blah, blah, 12:45
    [1] => blah
    [2] => blah
    [3] => 12:45
)
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • cleaned up stuff, would you like to comment on parsing TIME part ? – wadkar Oct 07 '11 at 10:02
  • how do I include `HH:MM` (or) `HH.MM` . Does this look correct to you : `(?:[01]?[0-9]|2[0-3])[:.][0-5][0-9])` ? – wadkar Oct 07 '11 at 10:32
  • @Sudhi Yes, that's correct. You can use `\d?\d[.:]\d\d` if you do not care for *validating* the numeric ranges. My variant accepts valid times from `00:00` through `23:59` only. There is a plethora of [date and time functions in PHP](http://php.net/manual/en/ref.datetime.php), you will find one that does validation etc. – Tomalak Oct 07 '11 at 10:39
  • thanks a lot, people over PHP chat room suggested strtotime, so I think I will skip the last bit of your regex and instead validate/create DateTime using strtotime – wadkar Oct 07 '11 at 10:43
  • 1
    update on last comment, seems like date_create() is much more useful than strtotime , thanks again for your regex – wadkar Oct 07 '11 at 10:56
  • @Sudhi Seems a good approach to me. If you already must do post-processing and validation, there is no need to do things in a regex that can be done more easily (and more readably) with the appropriate function. – Tomalak Oct 07 '11 at 11:19