Stripping the Ticket ID out of a String: “[Ticket ID: 9884219]”
-
30-06-2021 - |
Question
In my support ticket system, replies are sent to the user and the subject includes [Ticket ID: ######]
in it.
What could I use to strip the ###### out of there by supplying the entire title?
For example, a function that will turn this:
[Ticket ID: 600238] Forgot password, reset it please :(
into this:
600238
The subjects can start with any characters, though the ticket ID brackets are separated from the subject itself using a space.
Is there an RegEx to do this?
Solution
You can use preg_match
.
if (preg_match("/\[Ticket ID: (\d+)\]/", $input, $matches)) {
$ticket_id = $matches[1];
}
OTHER TIPS
'[' ']' have spacial meaning in regular expression, hence you have to escape them in the regular expression.
The regular expression you need is /\[Ticket ID: [\d]{6}\]/
You may want to use [\s]+ in between the words if not sure about number of spaces in between the words.
This Regular expression will give you whole Ticket ID: ######
And again you have to extract the number.
RegExp are very powerful and here you can learn more about RegExp here