PHP Velho Oeste 2024

str_starts_with

(PHP 8)

str_starts_withBir dizge belirtilen alt dizge ile başlıyor mu diye bakar

Açıklama

str_starts_with(string $samanlık, string $iğne): bool

samanlık dizgesine harf büyüklüğüne duyarlı bir arama uygulayıp iğne ile başlayıp başlamadığına bakar.

Bağımsız Değişkenler

samanlık

Araştırılacak dizge.

iğne

samanlık'ta aranacak parça.

Dönen Değerler

samanlık dizgesi needle ile başlıyorsa true, başarısızlık durumunda false döner.

Örnekler

Örnek 1 - Boş dizge '' kullanımı

<?php
if (str_starts_with('abc', '')) {
echo
"Tüm dizeler boş bir dize ile başlar";
}
?>

Yukarıdaki örneğin çıktısı:

Tüm dizeler boş bir dize ile başlar

Örnek 2 - Harf büyüklüğüne duyarlılık

<?php
$string
= 'The lazy fox jumped over the fence';

if (
str_starts_with($string, 'The')) {
echo
"Dizge 'The' ile başlıyor\n";
}

if (
str_starts_with($string, 'the')) {
echo
'Dizgenin baş tarafı: "the"';
} else {
echo
'Başta "the" yok çünkü harf büyüklüğü eşleşmedi';
}

?>

Yukarıdaki örneğin çıktısı:

Dizge 'The' ile başlıyor
Başta "the" yok çünkü harf büyüklüğü eşleşmedi

Notlar

Bilginize: Bu işlev ikil dosyalarla çalışırken dosya içeriğini değiştirmez.

Ayrıca Bakınız

  • str_contains() - Bir dizge içinde belirtilen alt dizgeyi arar
  • str_ends_with() - Bir dizge belirtilen alt dizge ile bitiyor mu diye bakar
  • stripos() - Harf büyüklüğüne duyarsız olarak bir alt dizgenin ilkinin konumunu bulur
  • strrpos() - Bir dizgede bir karakterin sonuncusunu bulur
  • strripos() - Bir dizgede harf büyüklüğüne duyarsız olarak bir alt dizgenin sonuncusunu bulur
  • strstr() - İlk alt dizgeyi bulur
  • strpbrk() - Belirtilen karakterleri bir dizge içinde arar
  • substr() - Dizgenin bir kısmını döndürür
  • preg_match() - Bir düzenli ifadeyi eşleştirmeye çalışır

add a note add a note

User Contributed Notes 3 notes

up
46
Paul Phillips
3 years ago
You can use this in PHP versions less than 8.

<?php
function str_starts_with ( $haystack, $needle ) {
  return
strpos( $haystack , $needle ) === 0;
}
up
13
jgusta
3 years ago
With credit to Paul Phillips for the original polyfill posted.

If you do not have PHP 8, you can use these functions to get the capability of the new string functions.

But! Remember to use a conditional check to make sure the function is not already defined.

<?php
// source: Laravel Framework
// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php
if (!function_exists('str_starts_with')) {
    function
str_starts_with($haystack, $needle) {
        return (string)
$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
    }
}
if (!
function_exists('str_ends_with')) {
    function
str_ends_with($haystack, $needle) {
        return
$needle !== '' && substr($haystack, -strlen($needle)) === (string)$needle;
    }
}
if (!
function_exists('str_contains')) {
    function
str_contains($haystack, $needle) {
        return
$needle !== '' && mb_strpos($haystack, $needle) !== false;
    }
}
?>

This keeps it from breaking in case you upgrade and forget that you added it. This is a good practice generally when using the global scope for your helper functions.
up
2
Reinder
2 years ago
In PHP7 you may want to use:

if (!function_exists('str_starts_with')) {
  function str_starts_with($str, $start) {
    return (@substr_compare($str, $start, 0, strlen($start))==0);
  }
}

AFAIK that is binary safe and doesn't need additional checks.
To Top