Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Parsing HTML using PERL or PHP Question

Options
  • 03-06-2006 9:58am
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hi All,

    I have an input in a HTML form which will always be "HH:MM Name".

    For good reason I cannot change that format or add another input to the HTML form - dont have time to explain.

    So, here is what Im trying to do in PERL:

    I go my $inputtime = param('timein'); This reads in "HH:MM Name".

    However I just want to extract the Hours and Minutes parts from the input. The reason I want to do this is to work out the time stamp on the input and convert all to one single value in minutes.

    Example:
    datein html input variable reads '01:10 John' - I want to be able to parse this and convert it into one single value in Minutes. I would also like to take the name John and store it in another variable also.

    my $minsin = '70';
    my $namein = 'John';

    Is there any way of splitting up the input string after it been parsed?:confused:

    I want to be able to compare input time to the current time.

    Any suggestions?

    Thanks,
    Finnpark


Comments

  • Registered Users Posts: 441 ✭✭robfitz


    finnpark wrote:
    Any suggestions?

    The output values are in $name and $mins, for more info read up on Binding Operators.
    my $input = "01:10 John";
    my $name;
    my $mins;
    
    if ($input =~ /(\d+):(\d+) (.*)/) {
            my $hour = $1;
            my $minute = $2;
    
            $name = $3;
            $mins = ($hour * 60) + $minute;
    }
    


  • Registered Users Posts: 21,611 ✭✭✭✭Sam Vimes


    finnpark wrote:

    Is there any way of splitting up the input string after it been parsed?:confused:


    yes, the split function. i can't remember the exact syntax but its something like this:


    ($hourMin, $name)=split(/ /, $datein);


    this will split the string at the space and put 01:10 into $hourMin and john into $name


    then if you want 01:10 in minutes you could split it at the : like this:
    ($hour, $min)=split(/:/, $hourMin);
    multiply $hours by 60, add it to $mins and robert's your mother's brother


  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    Use split . You can also define the components as part of a list.

    Try this

    @timestring=split(delimiter,$input);
    ($hour,$min,$sec)=@timestring;
    Now you print them out individually.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    For pig iron, in PHP you could also use substr:

    [PHP]if (isset($_POST)) {
    $time = substr($_POST,0,5);
    $name = trim(substr($_POST,6));
    }
    [/PHP]
    Valid input for this is leading zeros for hour and minute,
    the name is the rest of it after the single space, which in turn may or may not include spaces, trim is like chomp it just gets rid of leading and trailing spaces.
    You'd definitely want further validation to ensure data integrity.

    While you could do the lot with a regex I tend to go for a sequence of simple parsing and validation steps, this makes it more readable, easier to test and debug, and thereby easier for someone else to maintain if necessary.

    That said if everyone who'll need to hack the code is well up on regexes they are stunningly powerful and elegant, there's a certain joy in using them.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Cheers, all the suggestions here work:)

    Thanks a lot,
    Finnpark


  • Advertisement
Advertisement