VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-check-if-a-string-contains-a-substring-in-php/

⇱ How to check if a String Contains a Substring in PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to check if a String Contains a Substring in PHP ?

Last Updated : 23 Jul, 2025

Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.

Methods

Below are the following methods by which we can check if a string contains a substring in PHP:

1. Using str_contains() (PHP 8+)

Output:

Substring found!

2. Using strpos() (All PHP versions)

Output:

Substring found!

3. Case-Insensitive Search with stripos()

Output:

Substring found (case-insensitive)!

4. Using Regular Expressions with preg_match()

Best Practices

  • Use str_contains() for simple checks (if PHP 8+).
  • Always check strpos with !== false, not != false.
  • Use stripos() for case-insensitive searches.
  • Prefer preg_match() only for complex pattern matching.
Comment