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?

Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top