How to check if a String Contains a Substring in PHP ? (original) (raw)
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+)
PHP `
`
**Output:
Substring found!
2. Using strpos() (All PHP versions)
PHP `
`
**Output:
Substring found!
3. Case-Insensitive Search with stripos()
PHP `
`
**Output:
Substring found (case-insensitive)!
4. Using Regular Expressions with preg_match()
PHP `
`
Best Practices
- Use
str_contains()for simple checks (if PHP 8+). - Always check
strposwith!== false, not!= false. - Use
stripos()for case-insensitive searches. - Prefer
preg_match()only for complex pattern matching.