1

With a post request, I'm trying to return a booking number to my iOS App. It posts the current date as a string which is written to a file on my server.
The logic is, that whenever the date from the last entry does not match the date passed in the post request, the booking number starts from 01. If it is the same date, the booking number gets increased by one.

The text file I write my entries to looks like this:

2017-03-03, 1;
2017-03-03, 2;
2017-03-03, 3;
2017-03-03, 4;
2017-03-03, 5

I am using regex to find the last entry, comparing its date to the current and assigning the new booking number. Here is my code:

if (preg_match_all("/\d{4}-\d{1,2}-\d{1,2},\s\d\z/", $textFileString, $entryArray)) {
    if (preg_match_all("/\d{4}-\d{1,2}-\d{1,2}/", $entryArray[0][0], $dateArray)) {
        if ($dateArray[0][0] == $currentDate) { //$currentDate's value comes from the post request
            if (preg_match_all("/\d\z/", $entry[0][0], $numberArray)) {
                $bookingNumber = $output_array[0][0]+1;
            }
        } else {
            $bookingNumber = 1;
        }
    }
}

Note: I do successfully write to the file.

8
  • So what's not working? Commented Mar 3, 2017 at 9:53
  • If you are interested in a single match, why use preg_match_all? Use preg_match. Also, \z matches the very end of string, what if there is a trailing newline? Change into $. Commented Mar 3, 2017 at 9:54
  • Well when I read bookingNumber from the json response inside my app, I get the value that I assigned to $bookingNumber before these regex statements Commented Mar 3, 2017 at 9:55
  • @WiktorStribiżew I am trying to read the latest entry, which is obviously at the very end of the string. Commented Mar 3, 2017 at 9:58
  • What is the $currentDate value here? 2017-03-02? Commented Mar 3, 2017 at 10:04

1 Answer 1

1

I suggest switching to preg_match (since you only need to find a single match) and add capturing groups to the pattern so as to reduce the code a bit:

$textFileString = '2017-03-03, 1;
2017-03-03, 2;
2017-03-03, 3;
2017-03-03, 4;
2017-03-03, 5';
$currentDate = '2017-03-03';
if (preg_match("/(\d{4}-\d{1,2}-\d{1,2}),\s(\d{1,2})\z/", $textFileString, $entryArray)) {
    if ($entryArray[1] == $currentDate) { //$currentDate's value comes from the post request
        $bookingNumber = $entryArray[2]+1;
    } else {
        $bookingNumber = 1;
    }

}
echo $bookingNumber;

See the PHP demo.

Note you may also replace \z with $ since the $ anchor matches at the end of the string (or before a final LF in the string) by default.

Now, (\d{4}-\d{1,2}-\d{1,2}),\s(\d{1,2})\z contains two capturing groups that you may access via $entryArray[1] and $entryArray[2].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.