Need help with regex in preg_match_all ()
so I need to extract the ticket number "Ticket # 999999" from the string .. how to do it with regex.
my current regex works if i have more than one number in Ticket # 9999 .. but if i only have ticket # 9 it doesn't work please help.
the current regular expression.
preg_match_all('/(Ticket#[0-9])\w\d+/i',$data,$matches);
thank.
+3
source to share
1 answer
In your template [0-9]
matches 1 digit, \w
matches another digit, and \d+
matches 1+ digits, which requires 3 digits after #
.
Using
preg_match_all('/Ticket#([0-9]+)/i',$data,$matches);
This will match:
-
Ticket#
- literal stringTicket#
-
([0-9]+)
- group 1, spanning 1 or more digits.
PHP demo :
$data = "Ticket#999999 ticket#9";
preg_match_all('/Ticket#([0-9]+)/i',$data,$matches, PREG_SET_ORDER);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => Ticket#999999
[1] => 999999
)
[1] => Array
(
[0] => ticket#9
[1] => 9
)
)
+3
source to share