PHP: How can I make preg_match match only the first occurrence?

preg_match($pattern, $subject, $matches), $matches seems to be an array of all matches, I only need the first occurrence of the regex pattern, and I want the matching to stop right away once it finds a match and store it in $matches. Is it possible? Thank you.

I’m not talking about greedy match(.* and .*?), an example is as follows,

$str = 'abcd,abed,abfd,abgd';
preg_match("/ab.d/", $str, $match);

I want it to find only abcd and not any of the further matches, for further matches are useless to me and I assume would take more time to process.

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.