Search code examples
phpregexnumbersstripticket-system

Stripping the Ticket ID out of a String: "[Ticket ID: 9884219]"


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];
    }