I am wondering what other approaches you would take to do some simple string splitting in PHP. I am receiving a response from a SMS gateway where two of the interesting values are the code used and the users text message.
The code could be something like: Freetrip (lowercase, uppercase, mixed case)
The user message should in the best case scenario be e.g. like: Freetrip 12345 ($code “space” XXXXX).
Each X should be a digit between 1 and 5. Any other value/character should return an error. So the regex would be simplified as: chars=5 where each digit >=1 and <=5.
What I need to store at the end would be each of the 5 digits values.
My simplest approach would be to lowercase the entire message string and subtract the also lowercased code (plus the space) from the message string. That would leave me with the 5 digits which I would then split into 5 unique variables to store in the DB.
Now the tricky part is that the best case scenario described above may be hard to achieve. Typing a SMS is fiddly and typing errors occur easily. Some errors that may occur are the following:
- Too few or many digits.
- Non-digits characters.
- More characters after the XXXXX combination.
- Probably some other cases.
Any of those should return an individual error message which I can return to the sender.
I had some experience with SMS-platforms and AFAIK one error is enough. We tried to detect similar characters like small L and big I etc, or zero and O-letter. For example in your case you could write something like this:
the same you can do in any place of message pattern (if you want).
I did something like this (not sure – it was 5 years ago):
Sure in this case you can check “freetrip” or not, value is [1-5]{5} or not etc, and response your error as much as allows your imagination :). Good luck.
EDIT: The last one is updated and should fit your case. It’s better, because it will be very simple to create another service on it’s example if you’ll need it.