I have an issue whereby I need to return a number value that exists after a fixed sub string regardless of the other characters in the string around it. I am sort of there, because the following works.
$string = "Created Ticket_Id#234 (Some info)";
preg_match("/Ticket Id_#([0-9]+\s)/", $string, $res);
print $res[1];
Outputs: 234
But then when I get the string as follows.
$string = "Created Ticket_Id#234";
preg_match("/Ticket Id_#([0-9]+\s)/", $string, $res);
print $res[1];
Outputs: nothing
Is there a better way of doing this without looking for the white space?
Yes, just use
\b, the word boundary special character class, instead of\s.So your pattern would be:
Note that I pulled the special character class out of the capturing parenthesis as you need not capture it.
Also I noticed that your string had
Ticket_Id#234, while your regex hadTicket Id#234. You should change the underscore in your regex based on what the case really is.