PHP regex: ilk eşleşen dize bulmak

1 Cevap php

Ben çok çok uzun bir metinde ilk eşleşen dize bulmak istiyorum. Ben preg_grep () kullanın ve döndürülen dizinin ilk elemanını alabilir biliyorum. Ben sadece ilk maçı olması (ya da önceden tam olarak sadece bir maç olduğunu biliyorum) Ama eğer bunu böyle yapmak için verimli değildir. Herhangi bir öneri?

1 Cevap

preg_match()?

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.