Cara menggunakan str_contains php w3schools

It determines which part of the original string to replace with the replacement based on the start and length arguments.

Show

Since you told it to start at 0 and didn't include a length, it replaces the whole thing.

The function isn't useless, but the way you pass it 0 and nothing as the third and last argument makes it so.

In this short tutorial, we look at how to check if a PHP string contains a substring. We also look at the other relevant string manipulation methods.

However, in case you are here only for the solution use this .

Table of Contents - PHP string contains

  • Why check if a PHP string contains a substring?
  • Using
    $string = "Hire the top 1% freelance developers"
    if (str_contains($string, 'Hire')) {
        echo "PHP string contains 'Hire'";
    } else {
        echo "PHP string does not contain 'Hire";
    }
    
    //Output: "PHP string contains 'Hire'"
    
    2
  • Using
    $string = "Hire the top 1% freelance developers"
    if (str_contains($string, 'Hire')) {
        echo "PHP string contains 'Hire'";
    } else {
        echo "PHP string does not contain 'Hire";
    }
    
    //Output: "PHP string contains 'Hire'"
    
    3,
    $string = "Hire the top 1% freelance developers"
    if (str_contains($string, 'Hire')) {
        echo "PHP string contains 'Hire'";
    } else {
        echo "PHP string does not contain 'Hire";
    }
    
    //Output: "PHP string contains 'Hire'"
    
    4 and
    $string = "Hire the top 1% freelance developers"
    if (str_contains($string, 'Hire')) {
        echo "PHP string contains 'Hire'";
    } else {
        echo "PHP string does not contain 'Hire";
    }
    
    //Output: "PHP string contains 'Hire'"
    
    5
  • PHP string contains - Limitations and Caveats

Why check if a PHP string contains a substring?

While writing code, it is a common practice to check if a string contains a substring. Subsequently, each language comes with a few methods that can be used to do the same.

The string contains (str_contains) is the most commonly used method in PHP. However, the methods you chose would be decided based on your use cases.

If you are looking to just check if a string contains a substring you can use the str_contains function. However, if you are looking to check if a substring exists and to return its index, the stripos and strpos methods can be used.

With that out of the way let us look at all these methods in depth.

Using $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 2

The str_contains is a new function that was introduced in PHP 8. This method is used to check if a PHP string contains a substring.

The function checks the string and returns a boolean true in case it exists and false otherwise. However, keep in mind that str_contains is case-sensitive. Ensure that the substring used to search is in the right case, or you could use the strtolower() method.

Syntax of PHP $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 7:

str_contains(string, substring)

Parameters:

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: "PHP string contains 'Hire'"
8 - Required, the original string that you intend to search

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: "PHP string contains 'Hire'"
9 - Required, the substring that you are looking to check if the PHP string contains

Code & Explanation:

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: "PHP string contains 'Hire'"

The str_contains returns a boolean true or false and hence is often placed inside a conditional statement.

As aforementioned, the str_contains method is case-sensitive, here is an example of the same:

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: PHP string does not contain 'Hire"

To avoid such cases you could ensure that the substring used to search is in the right case, or you could use the strtolower() method.

Using $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 3, $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 4 and $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 5

This method is used to check and return the index of the substring in case the PHP string contains it. The following methods can be used to achieve this.

The stripos method checks if a PHP string contains a substring, and returns the index of the first occurrence of the substring.

However, the stripos method is case-insensitive, and if you are looking to case-sensitively check if a PHP string contains a substring you can use the strpos methods.

Lastly, unlike the previous methods, strrpos returns the last occurrence of the substring.

Syntax of $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 3, $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 4 and $string = "Hire the top 1% freelance developers" if (str_contains($string, 'Hire')) { echo "PHP string contains 'Hire'"; } else { echo "PHP string does not contain 'Hire"; } //Output: "PHP string contains 'Hire'" 5

//Syntax of stripos
stripos(string, substring, start)

//Syntax of strpos
strpos(string, substring, start)

//Syntax of strrpos
strrpos(string, substring, start)

Parameters:

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: "PHP string contains 'Hire'"
8 - Required, the original string that you are looking to search

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: "PHP string contains 'Hire'"
9 - Required, the substring that you are looking to check if the string contains

$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'hire')) {
    echo "PHP string contains 'Hire'";
} else {
    echo "PHP string does not contain 'Hire";
}

//Output: PHP string does not contain 'Hire"
8 - Optional, specific where the search for the substring should begin

Returns:

When used to check if a PHP string contains a substring all three methods return an index between 0 to n-1 and return false if the substring is not found.

However, given that 0 is also a falsy value, remember to use the identical or not identical operator === or !== and not the equal to operator == as 0 also would be considered as false.

Code & Explanation:

$string = "Hire the top 1% freelance developers"

$substring_index = stripos($string, "Top");
 
if($substring_index !== false) {
    echo "'Top' is a substring!";
}
if($substring_index !== false) {
    echo "'top' is a substring!";
}
// Output = 'Top' is a substring!
// Output = 'top' is a substring!
As stripos is case insensitive, it does not return a false when used to check if the PHP string contains the 'Top' substring. However, this is not the case when strpos or strrpos are used.

Now, let's look at a case where the index is 0.

$string = "Hire the top 1% freelance developers"

$substring_index = stripos($string, "Hire");

if($substring_index != false) {
    echo "'Hire' is a substring";
} else {
    echo "'Hire' is not a substring";
}
//Output = "'Hire' is not a substring"
 
if($substring_index !== false) {
    echo "'Hire' is a substring";
} else {
    echo "'Hire' is not a substring";
}
//Output = "'Hire' is a substring"

As you can see the initial != operator considers 0 as false and returns a negative output. This is why it is important to use identical operators while using these methods.

Apa yang dimaksud dengan string PHP?

String merupakan bentuk data yang biasa dipakai dalam bahasa pemrograman untuk keperluan menampung dan memanipulasi data teks, misalnya untuk menampung (menyimpan) suatu kalimat.

Strpos untuk apa?

Pengertian Fungsi strpos() Fungsi strpos() adalah fungsi bawaan PHP yang bisa digunakan untuk mencari posisi sebuah karakter atau sebuah string di dalam string lainnya. Hasil akhir fungsi ini adalah angka yang menunjukkan posisi karakter/string yang ingin dicari.