preg_match doesn't search the entire string

Sorry, I looked at manual:

$subject = "Re: [ABC-123-12345] blah";
$pattern = '/^\[[A-Z]{3}-\d{3}-\d{5}\]$/';
preg_match($pattern, $subject, $matches);
print("<pre>");
print_r($matches);

it works only if $subject contains only [ABC-123-12345] but it doesn’t search the entire $subject to find the regexp like above, which changes should I do? Of course I cannot use substr as I have no idea how $subject will be. please advice.

It works fine with preg_split but it returns all string, so how can I fetch ABC-123-12345 from the string?

Remove the ^ and $ from what you are searching for - they represent the beginning and end of the string and so with those there you only get a match if the [ABC-123-12345] is the entire string.

1 Like

Thanks a lot.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.