Oldukça basit ama ben tam sözdizimi çalışma alınamıyor.
Ben sadece 'için' ile bir dize varlıklar (harf duyarsız) eğer doğru veya yanlış bir onay görmek istiyorum.
Sadece bu ise, o zaman düz metin aramasını kullanabilirsiniz:
if (stripos("for the", $text) === 0) { // case-insensitive here
// string starts with "for the"
}
Ya da,
if (substr($text, 0, 7) == "for the")
Bana aşağıdaki yorumlarınızı merak var olan aslında hakkında hızlıdır, bu yüzden bazı kıyaslama yazdı.
İşte TLDR versiyonu:
strpos
gerçekten çok hızlı.strncmp
, güvenilir ve hızlı.preg_match
iyi bir seçenek değildir.İşte uzun versiyon bulunuyor:
return strpos($haystack, $needle) === 0
return preg_match("/^$needle/", $haystack) === 1
return substr($haystack, 0, strlen($needle)) === $needle
return strncmp($needle, $haystack, strlen($needle)) === 0
for ($i = 0, $l = strlen($needle); $i < $l; ++$i) {
if ($needle{$i} !== $haystack{$i}) return false;
}
return true;
İlginç noktalar:
strpos
on the long, entirely non-matching needle against the short haystack.
strpos
üst 11 kez kaydedilmiştir.strpos
iyi performansı vardı ama, bu uzun samanlıkta üzerinde uzun olmayan eşleme iğne tarafından aşağı tartılır oldu. Bunlar çoğu testlerden daha 5-10 kat daha yavaş oldu.strncmp
and en tutarlı hızlıydı.preg_match
, diğer fonksiyonlarından daha tutarlı yaklaşık 2 kat daha yavaş olduHaystack: 83 characters
______________________________________________________________
____________|__________ non-matching ___________|_______ matching ________|
| function | 1 | 5 | 82 | 83 | 1 | 5 | 83 |
|------------+--------+--------+--------+--------+--------+--------+--------|
| manual | 0.2291 | 0.2222 | 0.2266 | 4.1523 | 0.2337 | 0.4263 | 4.1972 |
| preg_match | 0.3622 | 0.3792 | 0.4098 | 0.4656 | 0.3642 | 0.3694 | 0.4658 |
| strncmp | 0.1860 | 0.1918 | 0.1881 | 0.1981 | 0.1841 | 0.1857 | 0.1980 |
| strpos | 0.1596 | 0.1633 | 0.1537 | 0.1560 | 0.1571 | 0.1589 | 0.1681 |
| substr | 0.2052 | 0.2066 | 0.2009 | 0.2166 | 0.2061 | 0.2017 | 0.2236 |
-----------------------------------------------------------------------------
Haystack: 10000 characters
______________________________________________________________
____________|__________ non-matching ___________|_______ matching ________|
| function | 1 | 5 | 82 | 83 | 1 | 5 | 83 |
|------------+--------+--------+--------+--------+--------+--------+--------|
| manual | 0.2275 | 0.2249 | 0.2278 | 4.1507 | 0.2315 | 0.4233 | 4.1834 |
| preg_match | 0.3597 | 0.3628 | 0.4147 | 0.4654 | 0.3662 | 0.3679 | 0.4684 |
| strncmp | 0.1886 | 0.1914 | 0.1835 | 0.2014 | 0.1851 | 0.1854 | 0.1989 |
| strpos | 0.1605 | 2.1877 | 2.3737 | 0.5933 | 0.1575 | 0.1597 | 0.1667 |
| substr | 0.2073 | 0.2085 | 0.2017 | 0.2152 | 0.2036 | 0.2090 | 0.2183 |
-----------------------------------------------------------------------------
Sen ^
bir dize başlangıcını belirtmek için kullanmak istiyorum:
$string_one = "For the love of Mike";
$string_two = "for the amazing reason.";
$match = preg_match("/^for the/i", $string_one); // Outputs 1
$match = preg_match("/^for the/i", $string_two); // Outputs 1
/i
arama harf duyarsız kılan bir parçasıdır.
Eğer olsaydı read the first example in the documentation Eğer cevabı görmüş olur.
if ( preg_match('/^for the/i', $sentence) )
{
// a match was found
}